遍历多个数组并插入SQL [英] looping through multiple arrays and inserting into SQL

查看:506
本文介绍了遍历多个数组并插入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[]">提交后进入$_GET['product']

如果我这样做:

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

返回以下内容:

0:9

1:10

2:11

3:12

与产品ID一样,我还有2个其他表单输入的结构相同,所以我的问题是如何遍历每个$ _GET($_GET['product']$_GET['linequantity']$_GET['lineprice'])以添加每个他们到多个SQL表行?另外,还需要输入其他记录,但是它们将是恒定的,因此,例如,如果要添加3行,则其他记录将被添加.对于三行中的每一行都是相同的.

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.

请帮助我,我疯了!

B.

该表称为:order_lines

The table is called: order_lines

值=>字段

$ _ GET ['产品'] => product_id

$_GET['product'] => product_id

$ _ GET ['linequantity'] =>单价

$_GET['linequantity'] => unit_price

$ _ GET ['lineprice'] =>数量

$_GET['lineprice'] => qty

$ unh => unh

$unh => unh

还有更多,但是,我可以从那里解决.

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

推荐答案

假设每个集合中的项目数相同,我将使用类似的方法:

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
}

这篇关于遍历多个数组并插入SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆