jeskey boneman
jeskey boneman

Reputation: 227

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

I trying to make a editable profile to users. They click on Edit button(form-post)That return the page with the editable information (only if isset($_POST["edit"]) in text areas, inputs and "complete editing" button. when i click complete editing. its need to start a function of update the new information to the database. but it`s not update its return a error of:

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

My code:

if(isset($_POST["cedit"]) && !empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"])){

    if($stmtq = $mysqli->prepare("UPDATE `sites`.`accounts` SET `fullname` = ? ,`description` = ? WHERE `id` = ? ") && !empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"])){
        $stmtq->bind_param("ssd", $_POST["fn"]." ".$_POST["ln"], $_POST["desc"], $_SESSION["user_id"]);
        $stmtq->execute();
        $stmtq->close();
    }
}

Upvotes: 2

Views: 59

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270677

You have a problem with operator precedence causing $stmtq to be a true or false boolean instead of the prepared statement it's intended to be.

This is due to the chain of && conditions packed into the same conditional. They happen before the assignment =. Add some (). Basically it amounts to:

// $x is always assigned a boolean
if ($x = (object && true && true))

Instead of the intended

// $x is assigned the object
if (($x = object) && (true && true))

To fix it:

// Wrap the assignment in its own () group to isolate from the && conditions
if (($stmtq = $mysqli->prepare("UPDATE `sites`.`accounts` SET `fullname` = ? ,`description` = ? WHERE `id` = ? ")) && !empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"]) {
  // Successful prepare() assignment and all other conditions
  // Proceed with bind_param()/execute()
}

Though it adds a couple of lines, it would be easier to read and less prone to these precedence problems to do the prepare() and assignment first, then validate the other conditions, or vice versa.

if (!empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"])) {
  if ($stmtq = $mysqli->prepare(.....)) {
     // All is well, proceed with bind_param()/execute()
  }
}

For the delicious details, here is PHP's operator precedence chart. The && logical operator has a higher precedence than the = assignment.

Upvotes: 5

Related Questions