php - 如何使用 mysqli 准备好的语句更新数据库?

标签 php mysql mysqli

我之前在查询中使用 mysqli,现在我将其转换为 mysqli 准备好的语句。我正在尝试使用上传图像更新特定数据,但不知道为什么会收到错误

mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in line 30

另外,如何在 mysqli 查询中执行查询,如mysqli_query($conn, $query)

下面是我的 UPDATE 查询的代码:

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!";

        }

}

在上面的代码中,有一个注释行说明如何执行查询。我怎样才能做到这一点?

当我点击“上传”按钮时,它说图像已上传,但没有出现在数据库中。这是为什么?

最佳答案

对于程序方式

$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);

尝试一下面向对象的风格

$query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("si", $imageData, $_GET['id']);
$stmt->execute();

关于php - 如何使用 mysqli 准备好的语句更新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37558760/

相关文章:

php - 当列 "A"中的数据匹配时,MySQL SUM 列 "B"中的数据

php - 我如何执行作为字符串存储在数据库中的php代码

python - MySQL :"Access denied to x@localhost' 使用 python

php - 如何使用 PHP 在 MySQL 数据库中存储和检索图像?

php - mySQL Object of class mysqli_result could not be converted to string问题

php - 跨站点请求伪造验证失败。所需参数 "state"缺少 Laravel Sammyk/Facebook 包

php - 如果自用户上次访问以来有新记录,则显示图像

php - 使用 PHP 从关系数据库获取数据时出错

php - 使用 mysqli 插入 mysql 数据库时遇到问题

php - 在 boolean 、mysqli_stmt::store_result 和 mysqli_stmt::close() 上调用成员函数 bind_param()