aron pascua aron pascua - 6 months ago 12
MySQL Question

How to UPDATE in database using mysqli prepared?

So I'm using mysqli before to my query and now I convert it to mysqli prepared. I'm trying to update a particular data with upload image and I don't know why I get the error

mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in line 30
and also how can I execute the query in mysqli query like
mysqli_query($conn, $query)


Below is the code of my UPDATE query:

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

$imageName = mysqli_real_escape_string($conn, $_FILES["latest_photo"]["name"]);
$imageData = mysqli_real_escape_string($conn, file_get_contents($_FILES["latest_photo"]["tmp_name"]));
$imageType = mysqli_real_escape_string($conn, $_FILES["latest_photo"]["type"]);

if (substr($imageType, 0,5) == "image") {

$query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, 'ss', $imageData, $_GET['id']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $updated_photo);

//HOW CAN I EXECUTE THE QUERY HERE?
echo "Image Uploaded";

}

else {

echo "Image is not uploaded!";

}

}


In the code above, there is a comment line on how to execute the query. How can I do that? thank you guys

EDITED:

Also when I click the button Upload, it says that the image is uploaded but does not appear in database. why is that?

Answer
 //for procedural way
  $query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?";
  $stmt = mysqli_prepare($conn, $query);
  // you should use i instead of s for id
  mysqli_stmt_bind_param($stmt, 'si', $imageData, $_GET['id']);
  mysqli_stmt_execute($stmt);   

//try this out in object oriented
   $query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?";
   $stmt = mysqli_prepare($conn, $query);
   $stmt->bind_param("si", $imageData, $id);
   $imageData=$imageName ;
   $id=$_GET['id'];
   $stmt->execute();