0

A mysqli prepared statement update query is returning a "Call to a member function bind_param() on a non-object" error in one php file ; however, it is running perfectly on a simpler test page.

The page on which the query is running fine:

    <?php require 'db/connect.php'; ?>
<?php
    session_start();
    if(isset($_SESSION["UserID"])){
    }else{
        header('Location: LogIn.php');
    }
?>
<?php
if (isset($_POST['Submit'])){
        $User = $_SESSION["UserID"];
        $updFName = $_POST['First'];
        $updLName = $_POST['Last'];
        $updEmail = $_POST['Email'];
        $updPW = $_POST['Pass'];

        $sql = "UPDATE user SET Fname = ?, Lname = ?, Email = ?, Password = ? WHERE UserID = ?";
        $stmt = $conn->prepare($sql);

        $stmt->bind_param('ssssi', $updFName, $updLName, $updEmail, $updPW, $User);
        $stmt->execute();

}
?>

<!DOCTYPE html>
<html>
<body>       
        <form name="form1" method="post" action="test.php">
        <input type="text" name="First" id="First">
        <input type="text" name="Last" id="Last">
        <input type="email" name="Email" id="Email">
        <input type="password" name="Pass" id="Pass">
        <input type="submit" name="Submit" id="Submit" value="Submit">
    </form>


        <?php echo $sql . '<br>'; ?>
        <?php echo $updFName . '<br>'; ?>
        <?php echo $updLName . '<br>'; ?>
        <?php echo $updEmail . '<br>'; ?>
        <?php echo $updPW . '<br>'; ?>
        <?php echo $User; ?>



</body>
</html>

The code on this page is returning the "Call to a member function bind_param() on a non-object" error. Any suggesting as to why? Thanks

    <?php require 'db/connect.php'; ?>
<?php
    session_start();
    if(isset($_SESSION["UserID"])){
    }else{
        header('Location: LogIn.php');
    }
?>
<?php
    $User = $_SESSION["UserID"];

    $sql = "SELECT UserID, Fname, LName, Email, Password FROM user WHERE UserID=?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i",$User);
    $stmt->execute();
    $stmt->bind_result($id, $fname, $lname, $email, $pw);

    if ($stmt->fetch()){
        $_SESSION['FirstName'] = $fname;
        $_SESSION['LastName'] = $lname;
        $_SESSION['Email'] = $email;
        $_SESSION['Password'] = $pw;

    }
    $stmt->close();
    $conn->close();
?>

<?php
    if(isset($_POST['Update'])){
        $User = $_SESSION["UserID"];
        $updFName = $_POST['First'];
        $updLName = $_POST['Last'];
        $updEmail = $_POST['Email'];
        $updPW = $_POST['Pass'];

        $sql = "UPDATE user SET Fname = ?, Lname = ?, Email = ?, Password = ? WHERE UserID = ?";
    $stmt = $conn->prepare($sql);

        $stmt->bind_param('ssssi', $updFName, $updLName, $updEmail, $updPW, $User);
        $stmt->execute();

        //header('Location: UpdateAccount.php');

        $stmt->close();
        $conn->close();
    }


?>
<!DOCTYPE html>
<html>
<head>
<title>Update Account</title>
<meta name="generator" content="Bluefish 2.2.5" >
<link href="css/Menu.css" rel="stylesheet" type="text/css">
<link href="css/Master.css" rel="stylesheet" type="text/css">
<meta charset="utf-8">
</head>
<body>
    <div class="Container">
        <div class="Header"></div>
        <div class="Menu">
            <div id="Menu">
                <nav>
                    <ul class="cssmenu">

                        <li><a href="#">Register</a></li>   

                        <li><a href="#">LogIn</a></li>  

                    </ul>
                </nav>
            </div>      
        </div>
        <div class="LeftBody"></div>
        <div class="RightBody">
            <form method="post" name="UpdateForm" id="UpdateForm" action="UpdateAccount.php">
                <div class="FormElement">
                    <input type="text" name="First" required="required" class="TField" id="First" value="<?php echo $_SESSION["FirstName"]; ?>">
                </div>
                <div class="FormElement">
                    <input type="text" name="Last" required="required" class="TField" id="Last" value="<?php echo $_SESSION["LastName"]; ?>">
                </div>
                <div class="FormElement">
                    <input type="email" name="Email" required=required class="TField" id="Email" value="<?php echo $_SESSION['Email']; ?>">
                </div>
                <div class="FormElement">
                    <input type="password" name="Pass" required="required" class="TField" id="Pass" value="<?php echo $_SESSION["Password"]; ?>">
                </div>
                <div class="FormElement">
                    <input type="submit" name="Update" class="Button" id="Update">
                    <?php
                        echo "User: " . $User . '<br>';
                        echo "updFName: " . $updFName . '<br>';
                        echo "updLName: " . $updLName . '<br>';
                        echo "updEmail: " .$updEmail . '<br>';
                        echo "updPW:" .$updPW . '<br>';
                    ?>
                </div>
            </form>        
        </div>
        <div class="Footer"></div>
    </div>
</body>
</html>
user73209
  • 1
  • 1
  • Your prepare statement failed. Check for `mysqli_error` after your prepare statement. – aynber Feb 08 '17 at 16:00
  • Or look in your server's error logs. – Jay Blanchard Feb 08 '17 at 16:01
  • 1
    You closed your connection after your select statement. Remove both close statements. – aynber Feb 08 '17 at 16:01
  • Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you cannot miss or ignore. – RiggsFolly Feb 08 '17 at 16:01
  • I found the answer to my own question. The problem was due to $stmt being used twice : a first time to load the data into $_SESSION['variables'] and a 2nd time with the UPDATE query. I inserted **$stmt->free_result()** before re-using $stmt, and now it is working just fine. – user73209 Feb 08 '17 at 17:48

0 Answers0