如何执行2x $ stmt-> execute(); ? 我收到了一个错误

I'm getting an error (so obvious error)

Call to a member function bind_param() on a non-object...

I want to check if user has provided his information when verifying his information. If no, it's a successful echo, but if yes, then I get that error... My php code :

<?php

    if(isset($_POST['configured'])){

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $username = $_SESSION['username'];

    $stmt = $mysqli->prepare("SELECT gender FROM members WHERE username =  ?");
    $stmt->bind_param('s', $username);
    $stmt->bind_result($gender);
    $stmt->execute();

    while ($stmt->fetch()){
        if(empty($gender)){
            echo "<font color='#DB4D4D'>Verification failed! Please provide your information and try again...</font>";
        } else {
             $_POST['configured'] = 1;
             $sql = "
             UPDATE members
             SET configured = ?
             WHERE username = ?
            ";
            $stmt = $mysqli->prepare($sql);
            $stmt->bind_param('ss', $_POST['configured'], $_SESSION['username']); // This is the error line
            $ok = $stmt->execute();

            if ($ok == TRUE) {
                echo "<p><font color='#00CC00'>Your information has been verified.</font><p>
                      <form action='index.php'> <input type='submit' class='buttondiv' value='Continue to Home Page'>";
            } else {
                echo "Error: " .$stmt->error;
            }
        }
    }
}
?>

Where is problem?

BTW: The error line is not incorrect itself.. there has to be something else going on...

You need to add session_start() to the top of the page. Your bind fails because there is no $_SESSION['username']

According to me you should replace:

$stmt->bind_param('ss', $_POST['configured'], $_SESSION['username']);
$stmt->bind_param('s', $_POST['configured']);
$stmt->bind_param('s', $_SESSION['username']);

mysqli->prepare() returned FALSE therefore $stmt->bind_param() was effectively something like FALSE->bind_param(), FALSE is not an object but a Boolean (not an object in PHP) hence the error message Call to a member function bind_param() on a non-object

mysqli->prepare() failing is always an option for various reasons.
Therefore always check the return value.

$stmt = mysqli_prepare(...);
if ( !$stmt ) {
  trigger_error( __LINE__ . ': '.print_r($mysqli->error_list, true), E_USER_ERROR );
}

Same with most of the other mysqli functions/methods, bind_param(), execute() ... always check the return value. If it's FALSE or NULL, something went wrong.
In short: add error handling!


Let's condense your script a little bit:

$stmt->execute();

while ($stmt->fetch()){
   ...
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('ss', $_POST['configured'], $_SESSION['username']);
    $ok = $stmt->execute();

You have an (active) statement in $stmt which you try to "overwrite" within the loop.
a) what would the next iteration of while ($stmt->fetch()) do?
b) that doesn't work with mysql; only one active statement per connection.