尝试更新MySQL表时出错

时间:2016-07-20 14:17:03

标签: php mysql sql prepared-statement

我正在尝试更新我的MySQL表,但我一直收到此错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近'INTO kh_comments(id,author,comment,'timestamp',abstract,   第1行的“date_of_abstract”

这就是我尝试更新表 kh_comments 的方式:

public function prepare($author, $arr) {
    $conn = new mysqli($this->servername, $this->username, $this->password, $this->db_name);

    foreach ($arr as $value) {
        if ($stmt = $conn->prepare("UPDATE INTO kh_comments (id, author, comment, timestamp, abstract, date_of_abstract) VALUES (?, ?, ?, ?, ?, ?)")) {
            $stmt->bind_param('dssdss', '1', $author, '', 0, $value['id'], $value['date']);
        }
        else {
            echo $conn->error;
        }
    }
}

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:1)

TIMESTAMP是MySQL中的受限制关键字。

为表格和字段名称使用反引号,即

"UPDATE `kh_comments` (`id`, `author`, `comment`, `timestamp`, ..."

https://dev.mysql.com/doc/refman/5.7/en/keywords.html

答案 1 :(得分:0)

使用这种方式。

$stmt = $conn->prepare("UPDATE kh_comments SET id=?, author=?, comment=?, timestamp=?, abstract=?, date_of_abstract=?";
if ($stmt) {             
    $stmt->bind_param('dssdss', '1', $author, '', 0, $value['id'], $value['date']);

}

有关详情,请点击Prepare Statement For Update Query

答案 2 :(得分:0)

我认为您的更新语法不正确尝试使用以下功能:

public function prepare($author, $arr) {
    $conn = new mysqli($this->servername, $this->username, $this->password, $this->db_name);

    foreach ($arr as $value) {
        if ($stmt = $conn->prepare("UPDATE kh_comments (id, author, comment, timestamp, abstract, date_of_abstract) VALUES (?, ?, ?, ?, ?, ?)")) {             
            $stmt->bind_param('dssdss', '1', $author, '', 0, $value['id'], $value['date']);
        }
        else {
            echo $conn->error;
        }       
    }
}

答案 3 :(得分:0)

根据你的评论

  

我想插入一个新行,如果没有存在与我给出的相同值的行。如果存在具有相同值的行,则只应更新

您需要使用INSERT.... ON DUPLICATE UPDATE语法来执行此操作

您也可以在循环外准备语句,然后使用新参数执行多次。

另外,由于您的一个列名是timestamp,这是一个MYSQL保留字,该列需要包含在反引号中,或者更好地重命名

public function prepare($author, $arr) {
    $conn = new mysqli($this->servername, $this->username, $this->password, $this->db_name);


    $sql = "INSERT INTO `kh_comments` 
                   (`id`, `author`, `comment`, `timestamp`, `abstract`, `date_of_abstract`) 
            VALUES (?, ?, ?, ?, ?, ?) 
            ON DUPLICATE 
              UPDATE `author` = ?, `comment`=?, `timestamp`=?, 
                     `abstract`=?, `date_of_abstract`=?
              WHERE `id` = ?";
    $stmt = $conn->prepare($sql);

    foreach ($arr as $value) {
        $stmt->bind_param('dssdssssdssd', 
                           '1', $author, '', NOW(), $value['id'], $value['date'],
                           $author, '', NOW(), $value['id'], $value['date'], '1' );
        $stmt->execute()
        if ( $stmt === false )
            echo $conn->error;
            exit;
        }
    }
}
  

当然,在您尝试存储的所有行中使用'1'作为id时,此代码只会运行一次。

答案 4 :(得分:-1)

你的UPDATE语句语法错误,供你参考我试图纠正你的代码。

public function prepare($author, $arr) {
    $conn = new mysqli($this->servername, $this->username, $this->password, $this->db_name);
    $sql="UPDATE kh_comments SET author= ?, comment = ?, timestamp= ?, abstract = ?, date_of_abstract= ? WHERE id= ?";
    foreach ($arr as $value) {    
        $stmt = $conn->prepare($sql);
        $stmt->bind_param('1', $author, '', 0, $value['id'], $value['date'],'dssdss');
        $stmt->execute();
        if ($stmt->errno) {
          echo "FAILURE!!! " . $stmt->error;
        }
        else echo "Updated {$stmt->affected_rows} rows";
    }
}

请检查我定义的参数的顺序。并根据需要进行更改