0

我正在尝试将图像路径插入现有数据库。下面的代码有效,但插入了一个新行。

$address= htmlentities($_SESSION['address']);
$city= htmlentities($_SESSION['city']);
$zip_code= htmlentities($_SESSION['zip_code']);

        $query =
          "INSERT INTO  property(name, size, type_picture, file_path, username) VALUES (?,?,?,?,?)";
        $conn = $db->prepare($query);
        if ($conn == TRUE) {
            $conn->bind_param("sisss", $myfile, $fileSize, $fileType, $path, $username);
            if (!$conn->execute()) {
                echo 'error insert';
            }else {
                echo 'Success!<br/>';
                echo '<img src="' . DISPLAY_PATH . $myfile . '"/>';
            }
        } else {
            die("Error preparing Statement");

当我尝试与上述相同但更新时,我得到“错误准备语句”。我需要更新空单元格(如果这很重要)。

$query =
   "UPDATE  property(name, size, type_picture, file_path, username) 
    SET(?,?,?,?,?) 
    WHERE address = '$address'  // with or without ''
          city = '$city' ";
$conn = $db->prepare($query);
            if ($conn == TRUE) {
                $conn->bind_param("sisss", $myfile, $fileSize, $fileType, $path, $username);
                if (!$conn->execute()) {
                    echo 'error insert';
                } // etc. etc.

太感谢了。试了一天,求大神帮忙。

4

2 回答 2

2

您的更新查询错误,请尝试以下操作:

$query = "UPDATE property SET name = ?, size = ?, type_picture = ?, file_path = ?, username = ?
WHERE address = ? AND city = ?"
$conn = $db->prepare($query);
            if ($conn == TRUE) {
                $conn->bind_param("sisss", $myfile, $fileSize, $fileType, $path, $username,$address,$city);
                if (!$conn->execute()) {
                    echo 'error update';
                } 
             }
于 2013-09-05T21:17:01.047 回答
2

你需要一个ANDorORWHERE语句中:

WHERE address = '$address' AND // with our without ''
      city = '$city' ";

我也不认为您应该将参数与字符串替换混合使用。品牌$address$city参数也是如此。

于 2013-09-05T20:56:28.103 回答