Jump to content

Prepared statement in if conditional not working as expected


AJinNYC

Recommended Posts

EDIT: Nevermind, my issue was that the function wasn't returning anything. The DB connection wasn't being returned out of the function.
 
I can't for the life of me see why this prepared statement inside of the if conditional is not working:

$dbConn=conn($host, $user, $password, $dbname, $_SESSION['websitename']);
/* set autocommit to off */
mysqli_autocommit($dbConn, false);
$executed_ok=null;
if($prepareuserdetails=mysqli_prepare($dbConn, "INSERT INTO `userdetails` (`fname`, `email`) VALUES (?,?)")){
die("Test");
if($binduserdetails=mysqli_stmt_bind_param($prepareuserdetails, 'ss', $regfname, $regemail)){
if($executeuserdetails=mysqli_stmt_execute($binduserdetails)){
$executed_ok1=true;
$lastID=mysqli_insert_id($dbConn);
var_dump($lastID);
}
else{
$executed_ok1=false;
}
}
else{
$dberrorshow=true;
}*/
}
else{
$dberrorshow=true;
}
if($prepareuser=mysqli_prepare($dbConn, "INSERT INTO `users` (`username`, `pass`, `id`) VALUES (?,?,?)")){
if($binduser=mysqli_stmt_bind_param($prepareuser, 'ssi', $regusername, $regpass, $lastID)){
if($executeuser=mysqli_stmt_execute($binduser)){
$executed_ok2=true;
}
else{
$executed_ok2=false;
}
}
else{
$dberrorshow=true;
}
}
else{
$dberrorshow=true;
}
//If there is a DB error, show message.
if($dberrorshow===true){
die('Database Error: We couldn\'t connect to the database. Please return to the <a href="//'.getDomain($_SESSION['websitename']).'">homepage</a>.');
}
else{
//If all queries executed okay, commit them; if not, rollback.
if($executed_ok1===true && $executed_ok2===true){mysqli_commit($dbConn);}
elseif($executed_ok1===false || $executed_ok2===false){mysqli_rollback($dbConn);}
}

I placed the die("Test") in there to see where it was failing, and it's failing at the prepare point... it immediately fails and heads to the error message. The query runs fine in phpMyAdmin.


For reference:

conndetails.php just has the connection details, dbname, username, password, and host.

DBFuncs.php

//Database Connection 
    function conn($host, $user, $password, $dbname, $websiteName){ 
    //MySQLi Connection 
    $dbConnection=mysqli_connect($host, $user, $password, $dbname); 

        /* check connection */ 
        if(!$dbConnection){ 
            if($_SESSION['siteisactive']=="no"){ 
            $errormsg='('.mysqli_connect_errno().') '.mysqli_connect_error(); 
            } 
            else{ 
            $domainname=getDomain($websiteName); 
            $errormsg='We couldn\'t connect to the database. Please return to the <a href="//'.$domainname.'">homepage</a>.'; 
            } 
        die('Connection Error: '.$errormsg); 
        } 
    }  
Edited by AJinNYC
Link to comment
Share on other sites

Your conn function is not returning the mysqli connection.  Change it to

//Database Connection 
function conn($host, $user, $password, $dbname, $websiteName)
{ 
    //MySQLi Connection 
    $dbConnection = mysqli_connect($host, $user, $password, $dbname); 

    /* check connection */ 
    if(!$dbConnection)
    { 
        if($_SESSION['siteisactive']=="no")
        { 
            $errormsg='('.mysqli_connect_errno().') '.mysqli_connect_error(); 
        } 
        else
        { 
            $domainname=getDomain($websiteName); 
            $errormsg='We couldn\'t connect to the database. Please return to the <a href="//'.$domainname.'">homepage</a>.'; 
        } 

        die('Connection Error: '.$errormsg); 
    }

    return $dbConnection; // return the connection 
}
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.