2012-05-16 30 views
4

如何編寫準備的更新語句?參考:mysqli::prepareMySQLi在PHP中準備了更新語句

我試圖寫它描述:

if ($stmt = $mysqli->prepare("UPDATE tblFacilityHrs SET title =? description = ? WHERE uid = ?")){ 
      $stmt->bind_param('sss', $title, $desc, $uid2); 

      //Get params 
      $title=$_POST['title']; 
      $desc=$_POST['description']; 
      $uid2=$_GET['uid'];  

$stmt->execute(); 
      $stmt->close(); 
    } 
    else { 
     //Error 
     printf("Prep statment failed: %s\n", $mysqli->error); 
    } 

錯誤:

Prep statment failed: 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 'description = ? WHERE uid = ?' at line 1 Edited row.

回答

13

你只是缺少一組列之間的逗號:

UPDATE tblFacilityHrs SET title = ?, description = ? WHERE uid = ? 
           ^^^^^^ 

當MySQL報告錯誤時,請使用之類的內容查看手冊中的語法以使用nea r',最常見的是'之前的字符,因爲這是你的錯誤發生的地方。

注意:您可能需要在設置輸入變量而不是之前調用bind_param()。我不記得MySQLi如何解析它們,以及它們何時綁定,但從邏輯上講,它在代碼中更有意義,先設置它們然後綁定。

//Get params 
$title=$_POST['title']; 
$desc=$_POST['description']; 
$uid2=$_GET['uid']; 

$stmt->bind_param('sss', $title, $desc, $uid2); 
2

你可能需要添加逗號:將其分配給變量之前

$stmt = $mysqli->prepare("UPDATE tblFacilityHrs SET title = ?, description = ? WHERE uid = ?" 
1

你要綁定的參數:

$title=$_POST['title']; 
$desc=$_POST['description']; 
$uid2=$_GET['uid']; 

$stmt->bind_param('sss', $title, $desc, $uid2); 

編輯:從頭開始的,它不無論參數是在你定義變量之前還是之後都有約束力(你每天都會學到新的東西!),似乎有所作爲,但是像邁克爾·塞d,從邏輯上講,首先定義它們是有意義的。

+0

是的,它首先讓它們變得更有意義! –