Skip to content
Advertisement

How to UPDATE in database using mysqli prepared statement?

I’m using mysqli before to my query and now I convert it to mysqli prepared statements. 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

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?

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

Advertisement

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 style

$query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("si", $imageData, $_GET['id']);
$stmt->execute();
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement