2016-03-30 26 views
0

我試圖通過PHP腳本執行更新到Sqlite3數據庫,但我遇到了一對我不明白的錯誤。執行基於變量的sqlite3更新通過php

PHP Warning: SQLite3::prepare(): Unable to prepare statement: 1, no such column: :column in C:\UwAmp\www\save.php on line 16, referer: http://localhost/Canvas.html

PHP Fatal error: Uncaught Error: Call to a member function bindParam() on boolean in C:\UwAmp\www\save.php:17\nStack trace:\n#0 {main}\n thrown in C:\UwAmp\www\save.php on line 17, referer: http://localhost/Canvas.html

這裏是我使用的PHP代碼的例子:

$url = $_POST['newURL']; 
$name = $_POST['saveName']; 
$index = $_POST['saveNum']; 
$user = $_POST['username']; 
$db = new SQLite3('Users.db'); 



$statement = $db->prepare("UPDATE Canvas_Saves SET ':column' = ':url' WHERE User = ':user'"); 
$statement->bindParam(':url', $url); 
$statement->bindParam(':user', $user); 
if($index ===1){ 
    $statement->bindParam(':column', 'Save1',SQLITE3_TEXT); 
} 
else if($index ===2){ 
    $statement->bindParam(':column', 'Save2',SQLITE3_TEXT); 
} 
else if($index ===3){ 
    $statement->bindParam(':column', 'Save3',SQLITE3_TEXT); 
} 
else if($index ===4){ 
    $statement->bindParam(':column', 'Save4',SQLITE3_TEXT); 
} 

$statement->execute(); 

第16行是$ DB->準備發言。 我試圖研究這個,但我沒有任何運氣。感謝您提供任何幫助。

+0

是怎樣的表結構? –

+0

表Canvas_Saves中的相關字段爲Save1,Save2,Save3,Save4。每個字段都是TEXT類型。 – Bromic

回答

0

您可以使用此

$url = $_POST['newURL']; 
$name = $_POST['saveName']; 
$index = $_POST['saveNum']; 
$user = $_POST['username']; 
$db = new SQLite3('Users.db'); 
$col=""; 
if($index ===1){ 
    $col="Save1"; 
} 
else if($index ===2){ 
    $col="Save2"; 
} 
else if($index ===3){ 
    $col="Save3"; 
} 
else if($index ===4){ 
    $col="Save4"; 
} 
$statement = $db->prepare("UPDATE Canvas_Saves SET ".$col."=:url WHERE User = :user "); 
$statement->bindParam(":url", $url); 
$statement->bindParam(":user", $user); 
$statement->execute(); 
+0

這給我留下了這個錯誤。 'PHP警告:SQLite3的::製備():無法準備聲明:1,鄰近" = ":語法錯誤在C:\\ UwAmp \\ WWW \\ save.php上線路26,引用者:HTTP: // localhost/Canvas.html' 26行代表準備語句。 – Bromic

+0

我相信我在索引時遇到了一個問題,我感謝您的幫助。 – Bromic