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

Php 如果绑定参数返回错误,则重定向,php,mysql,error-handling,prepared-statement,Php,Mysql,Error Handling,Prepared Statement,我正在尝试将我的页面重定向到另一个脚本,该脚本将显示所有错误消息(如果有)。以下是我试图做的: //Prepare statement $checkCode = $conn->prepare("SELECT COUNT(code) FROM subscribers WHERE code LIKE ?"); if(!$checkCode){ header('Location:'.$errorURL."?&notification=prepareFa

我正在尝试将我的页面重定向到另一个脚本,该脚本将显示所有错误消息(如果有)。以下是我试图做的:

//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
$errorURL
显然是声明的<代码>$code以前是从数据库检索的

此代码将我重定向到URL以
bindParamFailed
结尾的页面,因此错误来自
bind_param
语句。如果我把
If(!$checkCode){…}
部分注释掉,它就像一个符咒

为什么它不起作用?有什么想法吗


有没有其他(可能更智能的)方法来编程这样的自定义错误页?

这可能是因为区分大小写的变量名。首先使用$checkCode=。。。(驼峰格)和ghen检查是否($checkcode)(小写)。可能小写变量只是未定义…

$conn->prepare()
语句将在变量
$checkCode
失败时返回boolean
false
,对
$checkCode
对象进行的其他调用将不会修改该对象。它仍然是“真实的”,因此
if($checkCode)
没有意义,即使绑定/执行代码失败,也永远不会输入错误状态

相反,您需要检查这些方法调用返回的值是否成功,而不是再次检查
if($checkCode)
。我建议将它重构成一个由
if()
组成的链,每个链都有可能重定向到别处

//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...
这可以再重构一点,只调用一次
header()
,并在前面的错误上设置一个错误字符串

//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...
}

$checkcode
$checkcode
是两种完全不同的动物。@Fred ii-Uhhh。。。令人尴尬的那应该解决了,谢谢!不客气。事实上,马格内塔斯和我差不多是在同一时间发现的。如果他/她的答案可行,你可以接受。如果没有,也看看迈克尔的答案。@Fred ii-当然,只要等五分钟,我就可以接受答案了…:)很高兴问题解决了,cheers@MichaelBerkowski我刚刚又在做了一次,看起来它并没有完全按照它应该的方式工作。我故意拼错了一句话,想看看会发生什么。我得到的只是大量未定义变量的php错误消息。(我在拼写错误的语句后使用的语句)。我解决问题的方法是使用
转到这里
设置$err变量后,在此处添加
在下面的if语句之前:
如果(!empty($err)){…}
这是一个好方法/是否有任何问题it@MichaelBerkowski是的,只是想看看会发生什么。看到我更新的评论,我不小心按了enter键:S@Lupy好的,我想我明白为什么了-如果准备一开始失败,
if(empty($err)&&&!$checkCode…
不能短路,
$checkCode
将无法定义。我在上面嵌套了
if(empty($err))
。还有其他方法——我不喜欢使用
goto
的想法,尽管它可能是无害的。一些未来的人会阅读代码并批评你:)@MichaelBerkowski抱歉,仍然不起作用:(为什么
goto
是个坏主意?@MichaelBerkowski谢谢,现在我明白了!