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."?¬ification=prepareFailed");
die();
}else{
$checkCode->bind_param("s", $code);
if(!$checkcode){
header('Location:'.$errorURL."?¬ification=bindParamFailed");
die();
}else{
$checkCode->execute();
if(!$checkCode){
header('Location:'.$errorURL."?¬ification=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?
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."?¬ification=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."?¬ification=bindParamFailed");
die();
}
// Same here...
if (!$checkCode->execute()) {
header('Location:'.$errorURL."?¬ification=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."?¬ification=$err");
die();
}
else {
// All is well, store the variable
// and perform the rest of your code...
}
This might be because of case sensitive variable names. First you use $checkCode = ... (camel case) and ghen you check if ($checkcode) (lowercase). Maybe the lowercase var is simply undefined...