2013-03-17 81 views
0

我正在運行一個wamp服務器並創建一系列html網頁,用戶放入的數據然後通過PHP腳本提交給SQL數據庫。我用Id. set to INT auto increment Crime. and Suspect. set as VARCHAR(40)創建了一個數據庫表。我可以連接到數據庫,但現在當我嘗試插入我的錯誤:PHP腳本,SQL插入錯誤

'Problem with insert: 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 'case (Id, Case, Suspect) VALUES('NULL', 'test', 'test12')' at line 1'

所以我知道它與插入功能的問題,但是不能修復它!

<?php 
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$db = 'fid'; 

$case = (isset($_POST['case'])? $_POST['case']:"None"); 
$suspect= (isset($_POST['suspect'])? $_POST['suspect']:"None"); 

$conn = mysql_connect($dbhost,$dbuser,$dbpass); 
if (!$conn) 
die('Could not connect: ' . mysql_error()); 
mysql_select_db($db); 
mysql_query("INSERT INTO case 
(Id, Case, Suspect) VALUES('NULL', '$case', '$suspect') ") 
or die(' Problem with insert: ' . mysql_error()); 
echo 'Connected successfully'; 
mysql_close($conn); 
?> 

回答

2

casereserved keyword。如果你想有一個表,並columnj命名case你需要把它放在蜱:

INSERT INTO `case` (Id, `Case`, Suspect) VALUES('NULL', '$case', '$suspect') 

此外, Please, don't use mysql_* functions in new code。他們不再維護and are officially deprecated。查看red box?請改爲了解prepared statements,並使用PDOMySQLi - this article將幫助您決定哪個。如果您選擇PDO,here is a good tutorial

+0

感謝您在過去一天試圖解決的問題,我一直在圈子裏尋找幫助,只是看着PDO感謝您的建議! – James 2013-03-17 18:05:46