doucong3048 2012-07-16 00:08
浏览 15
已采纳

PHP不会添加我的SQL项目

I am using php and trying to add a item to my MySQL database.

If I use the following code it works:

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

However, if I use the following code it doesn't work:

$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());;

I get an error that says:

Could not perform select query - Unknown column 'ProductName1234' in 'field list'

The ProductName1234 is the data from $product_name and should be the data I am trying to add and not the column.

  • 写回答

2条回答 默认 最新

  • dstt1818 2012-07-16 00:12
    关注

    Change:

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

    to:

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

    You need to add ' to $product_name:

    VALUES (NULL,  '$product_name', 'item2')
                   ^             ^
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程