[英]Storing array in multiple mysql columns
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 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,但無法將其與多個變量一起使用。
馬丁,非常感謝
嗯,正如您所說,yu必須使用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
}
希望能幫助到你
在您的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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.