dongzong2017 2014-12-22 21:36
浏览 20
已采纳

如果绑定参数返回错误,则重定向

I am trying to have my page redirect to another script which will display all error messages if there should be any. Here is what I am trying to do:

//Prepare statement    
$checkCode = $conn->prepare("SELECT COUNT(code) FROM subscribers WHERE code LIKE ?");

    if(!$checkCode){
         header('Location:'.$errorURL."?&notification=prepareFailed");
         die();
    }else{
         $checkCode->bind_param("s", $code);
              if(!$checkcode){
                   header('Location:'.$errorURL."?&notification=bindParamFailed");
                   die();
              }else{
                   $checkCode->execute();
                   if(!$checkCode){
                        header('Location:'.$errorURL."?&notification=executeFailed");
                        die();
                   else{
                        //store result in a variable etc.}
     }

$conn and $errorURL are obviously declared. $code is previously retrieved from the database.

This code redirects me to the page whose URL ends in bindParamFailed, therefore the error comes from the bind_param statement. If I comment out the if(!$checkCode){...} part it work like a charm.

Why is it not working? Any ideas?

Are there any other (maybe more intelligent) ways of programming such a custom error page?

  • 写回答

2条回答 默认 最新

  • doudihuang7642 2014-12-22 21:44
    关注

    While the $conn->prepare() statement will return boolean false into the variable $checkCode on failure, the other calls you are making on the $checkCode object will not modify the object. It will still be "truthy", so if ($checkCode) isn't meaningful and the error states will never be entered, even if the bind/execute code fails.

    Instead you need to check the values returned by those method calls for success or failure instead of checking if ($checkCode) again. I'd recommend refactoring it into a chain of if(), each of which has the potential to redirect away.

    //Prepare statement    
    // If this fails, $checkCode will indeed be boolean false
    $checkCode = $conn->prepare("SELECT COUNT(code) FROM subscribers WHERE code LIKE ?");
    
    if(!$checkCode){
      header('Location:'.$errorURL."?&notification=prepareFailed");
      die();
    }
    
    // No need for the else because you cannot reach this code unless the previous
    // block was true -- it would redirect away on error.
    if (!$checkCode->bind_param("s", $code)) {
      header('Location:'.$errorURL."?&notification=bindParamFailed");
      die();
    }
    
    // Same here...
    if (!$checkCode->execute()) {
      header('Location:'.$errorURL."?&notification=executeFailed");
      die();
    }
    
    // All is well, store the variable
    // and perform the rest of your code...
    

    This could be refactored a little more to call header() only once and set an error string on the prior errors.

    //Prepare statement    
    $checkCode = $conn->prepare("SELECT COUNT(code) FROM subscribers WHERE code LIKE ?");
    
    if(!$checkCode){
      $err = "prepareFailed";
    }
    // On subsequent checks, test that the $err variable is still empty
    // If it isn't, that section will be skipped and you'll fall through to the
    // redirection header() call.
    if (empty($err)) {
      if (!$checkCode->bind_param("s", $code)) {
        $err = "bindParamFailed";
      }
    }
    if (empty($err)) {
      if (!$checkCode->execute()) {
        $err = "executeFailed";
      }
    }
    
    // Now, if the $err string is non-empty, redirect with the message
    if (!empty($err)) {
      header('Location:'.$errorURL."?&notification=$err");
      die();
    }
    else {
      // All is well, store the variable
      // and perform the rest of your code...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python