2015-07-11 141 views
1

錯誤:MySQL的語法錯誤#1064 - 插入

1064: 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 '(id, name, url, content, category) INTO articles VALUES (null, 'Names', 'names', 'text' at line 1

PHP代碼:

$sql = "INSERT (id, name, url, content, category) INTO articles 
VALUES (null, '$name', '$url', '$content', '$category')"; 
$insert = MySQL_Query($sql); 

和MySQL數據庫表:

id PRIMARY tinyint(20)  UNSIGNED AUTO_INCREMENT 
name  varchar(255) utf8_general_ci 
url   varchar(255) utf8_general_ci 
content  longtext  utf8_general_ci 
category varchar(255) utf8_general_ci 
+0

1064:你的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在'(id,name,url,content,category)附近使用正確的語法INTO articles VALUES(null,'Names','names','text'in line 1 – Jacob

+0

字段列表出現在表名後面。參見[here](http://www.w3schools.com/sql/sql_insert.asp) –

+0

如果這些變量來自用戶輸入,請確保您正在轉義它們,或者更好地使用準備好的語句。 – chris85

回答

2

你得到了查詢上半年倒退。首先你要說明要插入什麼表格,然後列出要接收值的字段。

$sql = "INSERT INTO articles (id, name, url, content, category) 
VALUES (null, '$name', '$url', '$content', '$category')"; 
+0

非常感謝你 – Jacob

1

您的語法不正確 - 應該是insert into table_name (column list) values (value list)。因此,在你的情況下:

$sql = "INSERT INTO articles (id, name, url, content, category) 
VALUES (null, '$name', '$url', '$content', '$category')"; 
+0

非常感謝你 – Jacob