dongsou0083 2010-08-14 15:03
浏览 94
已采纳

循环遍历多个数组并插入SQL

I have a form that once submitted some of its result are stored in arrays, example:

(The form has multiple lines with the same input names)

<select name="product[]"> once submitted goes into $_GET['product']

if I do:

// Product ID's
foreach($_GET['product'] as $name => $prodvalue) {
print "$name : $prodvalue<br>";
}

the following is returned:

0 : 9

1 : 10

2 : 11

3 : 12

As well as the Product ID's I have 2 other form input structured the same way, so my question is how do I loop through each of the $_GET's ($_GET['product'], $_GET['linequantity'] and $_GET['lineprice']) to add each of them to multiple SQL table rows? Also there will be other records that need to be entered, but, these will be constant, so for instance, if 3 rows are to be added then the other records will be the same for each of the 3 rows.

Please help me, I'm goin' nuts!

B.

EDIT:

The table is called: order_lines

Value => Field

$_GET['product'] => product_id

$_GET['linequantity'] => unit_price

$_GET['lineprice'] => qty

$unh => unh

There are more, but, i can work it out from there.

  • 写回答

2条回答 默认 最新

  • douji9816 2010-08-14 15:18
    关注

    Assuming you have the same number of items in each collection, I would go with something like this:

    $staticValue1 = $_GET['value1'];
    $staticValue2 = $_GET['value2'];
    
    foreach($_GET['product'] as $name => $prodvalue) {
      $name = my_escape($name);
      $prodvalue = my_escape($prodvalue);
      $linequantity = my_escape($_GET['linequantity'][$name]);
      $lineprice = my_escape($_GET['lineprice'][$name]);
      if (checkFormat($linequantity, $lineprince, $prodvalue, $name, $staticValue1, $staticValue2) {
        $query = "INSERT INTO <table> (qty, unit_price, product, name, static_value_1, static_value_2) VALUES ".
               "(".$linequantity.", '".$lineprice."', '".$prodvalue."', '".$name."', '".$staticValue1."', '".$staticValue2."')";
        $result = mysql_query($query); 
      }
    }
    
    function my_escape($input) {
      //filter chars that will cause problems in the database
      //  then return the filtered values; depends on db
      //  usually this means encoding quotes, at the very least
    
      return str_replace("'","/'",$input);
    }
    
    function checkFormat($linequantity, $lineprince, $prodvalue, $name, $staticValue1, $staticValue2) {
      //use a series of comparisons to return true or false
      //  based on whether or not ALL inputs match the expected range of values
    
      if (!(is_numeric($linequantity)) return false;
    
      ... //<-- more comparisons
    
      return true; // <-- reaching this means it did not fail on any checks
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里