简体   繁体   English

PHP / MYSQL插入数组Foreach循环

[英]PHP / MYSQL Insert Array Foreach Loop

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. 我有一个使用javascript的采购订单表格,用于向表中动态添加行并捕获多个订单项的数据。 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”,“数量”,“价格”作为每个列和数组。 . . Cust_PN[0] Cust_PN[1] and Cust_PN[2] for line items 1-3 respectively. 分别针对订单项1-3的Cust_PN [0] Cust_PN [1]和Cust_PN [2]。 I then have Qty[0], Qty[1], and Qty[2] and so on. 然后,我有Qty [0],Qty [1]和Qty [2],依此类推。

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. 但是,当我发布时,按照上面的示例,我仅发布了最后一个条目* [3]中的数组数据。

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,
  Qty,Sale_Price,UOM,Program,Required_Date) 
  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) {
  mysql_query($query1);
  }

I'm quite certain there are a number of issues in the above. 我很确定上面有很多问题。 . . thank you in advance. 先感谢您。

Your main issue is, declaring $query outside the loop. 您的主要问题是,在循环外声明$query 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. 它使它成为一个常量,并且也采用了当时未定义的值$a$b ,因此导致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. 也修复了您的SQL查询。 Correct syntax is 正确的语法是

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

You had put SELECT in place of VALUES 您已将SELECT替换为VALUES

Let me know if it works, and otherwise please tell what error it ives exactly. 让我知道它是否有效,否则请准确告知它存在什么错误。

foreach must be before the $query1 : foreach必须在$ 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,
    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]')" or die ('Error posting data');

    mysql_query($query1);

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM