-2

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

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

4 回答 4

1

您的 SQL 语法错误:

UPDATE `directory`
SET `First_Name` = :firstname, `Surname` = :surname, `Nicknames` = :nicknames
WHERE ID = :cid
于 2013-01-06T18:45:15.897 回答
1

这不是UPDATE查询的工作方式:

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

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

于 2013-01-06T18:45:25.453 回答
1

SQL 更新的语法与插入的语法不同

UPDATE `directory` 
   SET `First_Name` = :firstname,
       `Surname` = :surname,
       `Nicknames` = :nicknames
 WHERE `ID` = :cid
于 2013-01-06T18:45:37.507 回答
1

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

   update directory set firstname=:firstname, surname=:surname, ...;
于 2013-01-06T18:45:48.870 回答