使用PHP / PDO和MySQL更新记录

时间:2013-01-06 18:43:08

标签: php mysql pdo

我试图更新我的表中的记录但是我收到以下错误...

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(`First_Name`,`Surname`,`Nicknames`' at line 1Number of rows added: Go back 

我的代码如下....

// Connection data (server_address, database, name, poassword)
$hostdb = 'localhost';
$namedb = '_co_';
$userdb = '';
$passdb = '';

try {
  // Connect and create the PDO object
 $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $sql = "UPDATE `directory` (`First_Name`,`Surname`,`Nicknames`) 
      VALUES (:firstname, :surname, :nicknames)
      WHERE ID = :cid

      ;



      ";



 $statement = $conn->prepare($sql);
 $statement->bindValue(":cid", $cid);
 $statement->bindValue(":firstname", $firstname);
 $statement->bindValue(":surname", $surname);
 $statement->bindValue(":nicknames", $nicknames);

4 个答案:

答案 0 :(得分:1)

您的SQL语法错误:

UPDATE `directory`
SET `First_Name` = :firstname, `Surname` = :surname, `Nicknames` = :nicknames
WHERE ID = :cid

答案 1 :(得分:1)

这不是UPDATE查询的工作原理:

UPDATE `directory` SET `First_Name` = :firstname, `Surname` = :surname, `Nicknames` = :nicknames WHERE ID = :cid

我建议你研究一下sql的工作原理,因为只是猜测不会让你走得太远。您尝试使用的语法是INSERT语句。

答案 2 :(得分:1)

SQL更新的语法与插入

的语法不同
UPDATE `directory` 
   SET `First_Name` = :firstname,
       `Surname` = :surname,
       `Nicknames` = :nicknames
 WHERE `ID` = :cid

答案 3 :(得分:1)

您的更新声明有误。它应该是:

   update directory set firstname=:firstname, surname=:surname, ...;