2012-07-16 30 views
1

我使用PHP和试图将项目添加到我的MySQL数据库。PHP不会增加我的SQL项目

如果我使用下面的代码它的工作原理:

mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, 'item1', 'item2')") or die("Could not perform select query - " . mysql_error());; 

不过,如果我使用下面的代码它不工作:

$product_name=("tom"); 
mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, $product_name, 'item2')") or die("Could not perform select query - " . mysql_error());; 

我得到一个错误,指出:

无法执行选择查询 - '字段列表'中的未知列'ProductName1234'

ProductName1234是来自$product_name的数据,应该是我尝试添加的数据而不是列。

+0

首先,格式化你的代码! – 2012-07-16 00:09:40

+0

只是做了..我输入[代码],发现我没有错..现在所有固定。 – Aaron 2012-07-16 00:10:34

回答

3

变化:

mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, $product_name, 'item2')") or die("Could not perform select query - " . mysql_error());; 

到:

mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, '$product_name', 'item2')") or die("Could not perform select query - " . mysql_error()); 

您需要添加'$product_name

VALUES (NULL, '$product_name', 'item2') 
      ^   ^
+0

我试过了,它没有工作。 – Aaron 2012-07-16 00:15:09

+0

@Aaron,当然? – 2012-07-16 00:20:29

+0

我没有尝试过,但看起来像这样的作品,以及。对于那个很抱歉。 – Aaron 2012-07-16 00:24:47

4

当您插入这样的字符串,你需要围绕他们的报价,否则MySQL会认为你正试图从某处指定列从插入数据。

mysql_query("INSERT INTO intranet.product_form (id, ProductName, ProductInitiatedBy) VALUES (NULL, \"$product_name\", 'item2')"); 
+0

感谢堆..这做到了。 – Aaron 2012-07-16 00:15:45