MAK
MAK

Reputation: 41

PHP Update form using mysqli

I'm new to PHP, I need some help here. I have created a form called Home.php which accepts ID,name & file upload as inputs and it adds into mysql. Its adding successfully. I want to perform update operation so that if a user wants to update any of the fields. So what I want is it has to update these 3 fields in db. But I'm getting a following error in Process.php.

Error : Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement

Here's the relevant part of my code:

$sql = "UPDATE info SET id=?, name=?, upload=? WHERE id=?";
if(!($stmt = $mysqli->prepare($sql)))
{
  die("Unable to prepare statement");
}
else
{
  $stmt->bind_param("iss", $id, $name, $upload);    
  if($stmt->execute())
  {
    echo "Successfully updated";
  }
  else
  {
    die("Update failed");
  }
}

Upvotes: 0

Views: 290

Answers (2)

Saty
Saty

Reputation: 22532

this error is due to

 $sql = "UPDATE info SET id=?, name=?, upload=? WHERE id=?";

here in this query have bind 4 parameter

but in you bind only 3 param

$stmt->bind_param("iss", $id, $name, $upload); 

here it should be

$stmt->bind_param("issi", $id, $name, $upload,$id); 

AS per our discussion your final query would be

$sql = "UPDATE info name=?, upload=? WHERE id=?";
$stmt->bind_param("ssi",  $name, $upload,$id);

Upvotes: 0

xNeyte
xNeyte

Reputation: 622

You have 4 parameters needed for

$sql = "UPDATE info SET id=?, name=?, upload=? WHERE id=?";

Even if id should be the same, since you didn't give it name, you have to add it 2 times in your binding

$stmt->bind_param("issi", $id, $name, $upload, $id);

should work

You should check about adding named params in statements to avoid it.

Upvotes: 3

Related Questions