【问题标题】:Storing array in multiple mysql columns将数组存储在多个mysql列中
【发布时间】:2014-05-16 17:55:54
【问题描述】:

我正在努力从动态表单中存储我的数据 - 可以在填充期间添加/删除行。

这种形式的输出是数组:

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 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,但无法将其与多个变量一起使用。

非常感谢, 马丁

【问题讨论】:

    标签: php mysql arrays forms


    【解决方案1】:

    在你的 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
    
    }
    

    【讨论】:

    • 这就是我所需要的。太感谢了!马丁
    【解决方案2】:

    嗯,正如你所说,你必须使用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
     }
    

    希望对你有帮助

    【讨论】:

    • 是的,也谢谢你。我也为请求的数量而苦苦挣扎。所以 Awlad 的帮助对我帮助更大。无论如何,谢谢,奥斯卡。
    猜你喜欢
    • 2020-12-14
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 2017-11-16
    • 2012-11-26
    • 2015-08-23
    相关资源
    最近更新 更多