SQL Update表(Azure with PHP)

时间:2014-05-23 09:46:53

标签: php sql sql-server azure

不知怎的,我必须在某个地方犯错,似乎无法找到正确的解决方案,必须做错事。我想从PHP页面更新名为Celebrity的Azure SQL表,但它不会更新。你能帮我或告诉我我做错了吗

<?php 
 try {
$conn = new PDO( "sqlsrv:Server= $host ; Database = $db ", $user, $pwd);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e){
die(var_dump($e));
}

if(!empty($_POST)) {
try {
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$id = $_POST['id_celebrity'];

$sql_update = "UPDATE 
       Celebrity
    SET
        (FirstName, LastName)
    VALUES 
        (?,?)                      
    WHERE
       id_celebrity='$id'";

$stmt = $conn->prepare($sql_update);
$stmt->bindValue(1, $FirstName);
$stmt->bindValue(2, $LastName);
$stmt->execute();
}
catch(Exception $e) {
die(var_dump($e));
}
echo "Celebrity Updated in DB!";
}

?>

以下是使用中的说明,这是我从AZURE服务器返回的内容:

object(PDOException)#3 (8) { ["message":protected]=> string(97) "SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '('." ["string":"Exception":private]=> string(0) "" ["code":protected]=> string(5) "42000" ["file":protected]=> string(62) "D:\home\site\wwwroot\CelebrityOverview\CelebrityEditResult.php" ["line":protected]=> int(57) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(62) "D:\home\site\wwwroot\CelebrityOverview\CelebrityEditResult.php" ["line"]=> int(57) ["function"]=> string(7) "execute" ["class"]=> string(12) "PDOStatement" ["type"]=> string(2) "->" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> array(3) { [0]=> string(5) "42000" [1]=> int(102) [2]=> string(80) "[Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '('." } }

1 个答案:

答案 0 :(得分:1)

请改为尝试:

$sql_update = "UPDATE Celebrity
SET FirstName = ?, LastName = ?             
WHERE id_celebrity='$id'";

在每个列中为逗号分配值,UPDATE标记与INSERT不同。