在多个mysql列中存储数组

时间:2014-04-04 12:11:40

标签: php mysql arrays forms

Iam努力从动态表单存储数据 - 在填充行时可以添加/删除行。

此表单的输出是数组:

Array ( [service] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [desc] => Array ( [0] => Complete service - description [1] => Half Service - description [2] => Break Service - description ) [price] => Array ( [0] => 1000 [1] => 500 [2] => 800 ) [income_sum] => 2300 [odeslat] => Odeslat )

表格定义:

Column Type Comment
id  int(11) Auto Increment   
order_id    int(11) NULL     
servis_id   tinyint(4) NULL  
price   int(11) NULL     
desc    text NULL

FORM定义:

<form action="test.php" method="get" id="form">
<div class='container'>
<select name='service[]'>
<option value=""></option>
<option value="1">Complete Service</option>
<option value="2">Half Service</option>
<option value="3">Break Service</option>
</select>
Description
<input type='text' name='desc[]'>
Price
<input class='income_count' type='text' name='price[]'>
<input type="submit">
</form>

我不知道如何使用多个值存储这些多列。 我会使用FOREACH,但未能将它与多个变量一起使用。

非常感谢, 马丁

2 个答案:

答案 0 :(得分:0)

好吧,正如你所说,你必须使用for

 for($i=0;$i<count($values['service']);$i++)
 {
    $service = $values['service'][$i];
    $desc = $values['desc'][$i];
    $price = $values['price'][$i];

    //Now, perform the insert
 }

希望有所帮助

答案 1 :(得分:0)

你的test.php中的

得到一个计数来检查你有多少表单数据集并循环遍历它:

$total = count($_REQUEST['price']); // get total
$arrService = $_REQUEST['service'];// get  services array
$arrDesc = $_REQUEST['desc']; //get desc array
$arrPrice = $_REQUEST['price']; // get price array 

for($i=0;$i<$total;$i++){  
  $service = $arrService[$i];
  $dsc = $arrDesc[$i];
  $price = $arrPrice[$i];
  //insert those data into database

}