2012-06-15 22:21
浏览 49

PHP / MYSQL插入数组Foreach循环

This one has me pretty rattled so I thank you in advance for your assistance. There seem to be a number of walkthroughs on this topic but it seems I may be skinning this cat a bit different. . . .

I have a purchase order form that I'm using javascript to dynamically add rows to a table and capture data for multiple line items. I'm then collecting data for each column in an array. For example I have "Cust_PN", "Qty", "Price" as columns and arrays for each. . . Cust_PN[0] Cust_PN[1] and Cust_PN[2] for line items 1-3 respectively. I then have Qty[0], Qty[1], and Qty[2] and so on.

I can get this to echo properly without issue. However, when I go to post I am only posting the array data from the last entry *[3] per my example above.

I currently have the following code/query. . . again any help would be much appreciated.

$query1 = "INSERT INTO SO_Items (Timestamp,SO_Num,SO_Rev,SO_Line_Item,Cust_PN,Cust_PN_Rev,My_PN,My_PN_Rev,Description,
  SELECT NOW(),'$SO_Num','$SO_Rev','$SO_Line_Item[$a]','$Cust_PN[$a]','$Cust_PN_Rev[$a]','$My_PN[$a]','$My_PN_Rev[$a]','$Description[$a]','$Qty[$a]','$Sale_Price[$a]','$UOM[$a]','$Program[$a]','$Required_Date[$a]'" or die ('Error posting data');

foreach($Cust_PN as $a => $b) {

I'm quite certain there are a number of issues in the above. . . thank you in advance.

图片转代码服务由CSDN问答提供 功能建议

这个让我非常慌张,所以我提前感谢你的帮助。 似乎有很多关于这个主题的演练,但似乎我可能会给这只猫画上一些不同的东西。 。 。 。

我有一个采购订单表单,我正在使用javascript动态地向表中添加行并捕获多个订单项的数据。 然后我收集数组中每列的数据。 例如,我将“Cust_PN”,“Qty”,“Price”作为列和每个数组。 。 。 Cust_PN [0] Cust_PN [1]和Cust_PN [2]分别用于行项目1-3。 然后我有Qty [0],Qty [1]和Qty [2],依此类推。

我可以正确回应这个问题而没有问题。 但是,当我发布帖子时,我只发布了上一个条目* [3]中的数组数据。

我目前有以下代码/查询。 。 。 再次感谢任何帮助。

  $ query1 =“INSERT INTO SO_Items(Timestamp,SO_Num,SO_Rev,SO_Line_Item,Cust_PN,Cust_PN_Rev,My_PN,My_PN_Rev,Description,
 SELECT NOW(),'$ SO_Num','$ SO_Rev','$ SO_Line_Item [$ a]','$ Cust_PN [$ a]','$ Cust_PN_Rev [$ 一个] ' '$ My_PN [$ A]', '$ My_PN_Rev [$ A]', '$说明[$ A]', '$数量[$ A]', '$ SALE_PRICE [$ A]','  $ UOM [$ a]','$ Program [$ a]','$ Required_Date [$ a]'或死亡('错误发布数据'); 
($ Cust_PN as $ a  => $ b){
 mysql_query($ query1); 

我很确定上面有很多问题。 。 。 提前谢谢你。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqj96395 2012-06-15 22:40

    Your main issue is, declaring $query outside the loop. It makes it a constant, and that too, takes values $a and $b which are that time, undefined, so results in invalid syntax for SQL.

    foreach($Cust_PN as $a => $b) {
       $query1 = "INSERT INTO SO_Items (Timestamp, SO_Num, SO_Rev, SO_Line_Item, Cust_PN, Cust_PN_Rev, My_PN, My_PN_Rev, Description, Qty, Sale_Price, UOM, Program, Required_Date) VALUES (NOW(), '$SO_Num', '$SO_Rev', '$SO_Line_Item[$a]', '$Cust_PN[$a]', '$Cust_PN_Rev[$a]', '$My_PN[$a]', '$My_PN_Rev[$a]', '$Description[$a]', '$Qty[$a]', '$Sale_Price[$a]', '$UOM[$a]', '$Program[$a]', '$Required_Date[$a]');";
       $q = mysql_query($query1) or die ('Error posting data');

    Try that. Fixed your SQL query too. Correct syntax is

    INSERT INTO table_name (column1, column2, column3,...)
    VALUES (value1, value2, value3,...)

    You had put SELECT in place of VALUES

    Let me know if it works, and otherwise please tell what error it ives exactly.

    点赞 打赏 评论
  • drkxgs9358 2012-06-15 22:31

    foreach must be before the $query1 :

    foreach($Cust_PN as $a => $b) {
        $query1 = "INSERT INTO SO_Items (Timestamp,SO_Num,SO_Rev,SO_Line_Item,Cust_PN,Cust_PN_Rev,My_PN,My_PN_Rev,Description,
        VALUES(NOW(),'$SO_Num','$SO_Rev','$SO_Line_Item[$a]','$Cust_PN[$a]','$Cust_PN_Rev[$a]','$My_PN[$a]','$My_PN_Rev[$a]','$Description[$a]','$Qty[$a]','$Sale_Price[$a]','$UOM[$a]','$Program[$a]','$Required_Date[$a]')" or die ('Error posting data');
    点赞 打赏 评论

相关推荐 更多相似问题