php - 将数组存储在多个mysql列中

标签 php mysql arrays forms

我很难从动态表单中存储数据 - 可以在填充行时添加/删除行。

此表单的输出是数组:

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

非常感谢, 马丁

最佳答案

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

}

关于php - 将数组存储在多个mysql列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22862460/

相关文章:

php - 如何在 SQL 中管理产品选项和仓库项目之间的关系

c# - 在 Silverlight (C#) 中访问 MySQL 数据库

php - wordpress 迁移后,我的端口被阻止并且站点拒绝连接

javascript - 如何使用 JavaScript 或 jQuery 更改数组内对象的值?

php - 如何将月份添加到特定日期

PHP 和我当前的 HTML w/CSS 文件

php - 为 m :m relationship (pupil-evening_activity) consistently 插入行

mysql - vb.net 中的数据读取器

javascript - 只打印偶数

javascript - 将 getElementById 返回值视为数组