插入查询后重定向,执行不起作用 [duplicate]

Redirecting after inserting query execution not working [duplicate]

提问人:Amahle Lethabo 提问时间:1/6/2021 最后编辑:DharmanAmahle Lethabo 更新时间:1/6/2021 访问量:38

问:

我一直在尝试在插入数据库后重定向,但它不起作用。插入工作正常,但它没有重定向,这是我的代码

  if (count($errors) == 0) {
    
    $query = "INSERT INTO investment (userid, amount, status, hashID, plan) 
              VALUES('$uidb', '$amount', '$status', '$id', '$plan')";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      exit(header('location: https://google.com'));
    }else {
        array_push($errors, "Invalid Sort Code");
    }
  }
php mysqli 插入

评论

0赞 AbraCadaver 1/6/2021
在该代码之前,您有任何输出吗?
1赞 AbraCadaver 1/6/2021
此外,如果条件将始终失败,因为它将 .尝试 php.net/manual/en/mysqli.affected-rows.php0
0赞 Amahle Lethabo 1/6/2021
不,代码先生之前没有输出
0赞 Honk der Hase 1/6/2021
exit(header('location: https://google.com'));-为什么?!?请将其分开header('Location: ...'); exit;
0赞 Dharman 1/6/2021
@LarsStegelitz 你为什么会推荐这个。一气呵成更简单。您的建议根本不是改进。

答:

-1赞 Alexander Dobernig 1/6/2021 #1

正如 @AbraCadaver 和 @Lars Stegelitz 评论的那样,试试这个:

但更好的是,通过为投注者安全准备好的声明来做到这一点。

if (count($errors) == 0) {

$query = "INSERT INTO investment (userid, amount, status, hashID, plan) 
          VALUES('$uidb', '$amount', '$status', '$id', '$plan')";
$results = mysqli_query($db, $query);
if (mysqli_affected_rows($db)) == 1) {
  header('location: https://google.com');
}else {
    array_push($errors, "Invalid Sort Code");
}

}

评论

0赞 Amahle Lethabo 1/6/2021
非常感谢,这成功了,上帝保佑你