MySQLi update and die(); and exit(); not working

637 Views Asked by At

I'm trying to replace my MySQL with MySQLi, and i get results - that's ok but when it comes to update MySQL my code fails ( I updated only the MySQL part)

inc_mysql_connect.php

$db = mysqli_connect($host, $user, $pass, $dbase) or die (); 

inc_global_functions.php

if($_POST['costAlertTrue'] != "") {

// CHECKS AND IF ERROR - REDIRECT BACK
if(..) {$_SESSION['status'] ="E1"; header("Location: ./?page=cAlert"); exit();}
if(..) ...

// SQL
$sql = "UPDATE ".$dbprfx."_users SET costAlertVar = ? WHERE connection = ?";
$eintrag = $db->prepare($sql);
if ($eintrag->error()) { print($eintrag->error()); }
$eintrag->bind_param('ss',$costAlertVar,$userConnection);
$eintrag->execute();
$eintrag->close();

// REDIRECT 
$_SESSION['status'] ="on"; header("Location: ./?page=cAlert");

} // End

Can Anyone find an Error in this piece of code?

Notice

if($_POST['costAlertTrue'] != "") {
die('XX'); exit('XX');
}

It seems that die(); exit(); or any other errors are not working in the if clause

4

There are 4 best solutions below

0
On

Thx for all your help, i tried all your tips but suddenly it worked

$sql = "UPDATE ".$dbprfx."_users SET costAlertVar = ? WHERE connection = ?";
$eintrag = $db->prepare( $sql );
$eintrag->bind_param('ss',$costAlertVar,$userConnection);
$eintrag->execute();
$eintrag->close();
3
On

Doesnt this mean if no error die

if (!$eintrag->error()) { die('Not Working'); }

Remove the !

1
On

Line $eintrag->error(); {die('Not Working');} will die every time, even if everything was good, because PHP will see statement $eintrag->error() and code block {die('Not Working');}.

You should use is like this:

if (!$eintrag->error()) { die('Not Working'); }
0
On

what happens if you replace your main if statement with the following? I've made it fairly simple so as long as you are passing the $db variable properly, this should give you back SOME kind of result.

if($_POST['costAlertTrue'] != "") {
    // CHECKS AND IF ERROR - REDIRECT BACK
    if(..) {$_SESSION['status'] ="E1"; header("Location: ./?page=cAlert"); exit();}
    if(..) ...

    // SQL
    $sql = "UPDATE ".$dbprfx."_users SET costAlertVar = ? WHERE connection = ?";


    //$eintrag = $db->prepare($sql);
    //if ($eintrag->error()) { print($eintrag->error()); }
    if($db->connect_errno > 0){
            die("Error connecting to db.");
    }

    //$eintrag->bind_param('ss',$costAlertVar,$userConnection);
    //$eintrag->execute();
    if(!$result = query($sql)){
            die("Error running query");
    }
    //$eintrag->close();
    $db->close();

    // REDIRECT 
    $_SESSION['status'] ="on"; header("Location: ./?page=cAlert");

} // End

Also I noticed that you are using an or die statement in your mysqlic_connect statement that contains no error text. If in fact you can't connect there, your script is dying at the start with no output. This would also be a cause of the problem.