Mysqli prepare语句将图像插入到数据库中

时间:2014-12-17 01:04:12

标签: php mysql mysqli

我有一个正常的sql语句,直到我试图将其更改为sql prepared语句。

以下是旧插页:

$sql = "INSERT INTO items (seller, post_date, expiration_date, image, description, name, category, startBid, buyPrice, minPrice, sold) VALUES ('$id_user', NOW(), '$postDate', '$image', '$description', '$itemName', 0, '$startBid', '$buyNow', '$reservation', 0)";
$db->send_sql($sql);

我试图在这里做好准备:

$stmt = $mysqli->prepare("INSERT INTO items (seller, post_date, expiration_date, image, description, name, category, startBid, buyPrice, minPrice, sold) VALUES (?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, 0)";
$stmt->bind_param("isbssiddd", $id_user, $postDate, $image, $description, $itemName, $itemCategory, $startBid, $buyNow, $reservation);
$stmt->execute();
$stmt->close();

两个语句都会执行,但它们会导致数据库中的图像值不同。第一个语句的图像值是我期望的并且可以检索/显示。使用预准备语句输入的图像显示放入数据库中的内容,但不会显示为有效图像。图像区域是长斑。我哪里错了?谢谢!

1 个答案:

答案 0 :(得分:3)

找出问题所在。这就是我获取$ image的方式:

if (!empty($_FILES['inputPic']['tmp_name']))
{
    if ($_FILES['inputPic']['type'] == "image/jpeg" || $_FILES['inputPic']['type'] == "image/jpg" || $_FILES['inputPic']['type'] == "image/png")
    {
        if ($content = file_get_contents($_FILES['inputPic']['tmp_name']))
        {
            $image = addslashes($content);
        }
    }
}

我需要在旧的mysql语句中使用addslashes函数,但现在不需要它。让它$image = file_get_contents($_FILES['inputPic']['tmp_name'])解决问题