2014-09-26 146 views
0

我一直在檢查自己的語法,但我似乎無法看到錯誤。我對這一切都比較陌生,希望可以提供幫助。我得到的錯誤是使用PHP將數據輸入到MySQL數據庫時遇到困難

「您的SQL語法錯誤;請查看與您的MySQL服務器版本相對應的手冊,以找到在''ID','Name','選項」, '兩岸')VALUES(NULL, '丹尼斯', '雞', '泥Potat' 第1" 行

我的代碼如下:

<?php 
include 'connect.php'; 

$name = $_POST['inputName']; 
$opt = $_POST['inputOption']; 
$side = $_POST['inputSides']; 

if(!$_POST['Submit']) { 
    echo "Please fill out the form."; 
    header('Location: index.php'); 
} else { 
    mysql_query("INSERT INTO people ('ID','Name','Option','Sides') 
       VALUES(NULL,'$name','$opt','$side')") or die(mysql_error()); 
    echo "User has been added."; 
    header('Location: index.php'); 
    } 
?> 

回答

1

您使用的是錯誤identifiers爲您的列:

('ID','Name','Option','Sides') 

刪除引號或用反引號將它們包裝起來。

(`ID`,`Name`,`Option`,`Sides`) 

另外,您現在的代碼是SQL injection
使用mysqli with prepared statementsPDO with prepared statements,他們更安全

另一件事;如果你想輸入撇號,請使用stripslashes(),包括mysql_real_escape_string()。這個場合可能會很好地呈現出來; 瞭解

  • 否則,SQL會拋出另一個錯誤。
+1

非常感謝。我甚至沒有意識到。現在一切都完美無瑕。 – 2014-09-26 02:06:32

+0

@JohnMoran不客氣John。 – 2014-09-26 02:06:47

+0

@JohnMoran你可以接受我的答案並將其標記爲已解決,這裏是如何** http://meta.stackexchange.com/a/5235/**並歡迎來到Stack。 – 2014-09-26 10:25:04

相關問題