用Session更新SQL

时间:2013-09-05 20:54:44

标签: php mysql sql mysqli

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

$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.

非常感谢你。试了一天,需要一些帮助。

2 个答案:

答案 0 :(得分:2)

AND语句中需要ORWHERE

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

我也认为你不应该将参数与字符串替换混合在一起。同时制作$address$city参数。

答案 1 :(得分:1)

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

$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';
                } 
             }