如何将此表单的所有值作为数组提交并在发布到 SQL db 时循环?
How to submit all the values of this form as an array and loop through when posting to SQL db?
我有一个包含一些文本输入的表格,我想在填写完这些表格后将其提交给数据库。我是 PHP 的新手,所以我不知道如何将所有值保存到数组中并提交。
这是我的 table 包装形式:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
</br>
<form action="" method="post">
<div id="wrapper">
<button type="submit" class="btn" name="add_device">Add devices</button>
</div>
</br>
<table id="ble_table" class="table table-bordered table-striped" cellspacing="0" width="100%">
<tr>
<th>No.</th>
<th>Serial no.</th>
<th>IMEI</th>
</tr>
<tr>
<td>1</td>
<td><input type="text" placeholder="Serial no." class="inputs" name="serial_no" id="serial_no" /></td>
<td><input type="text" placeholder="IMEI" class="inputs lst" name="imei" id="imei" /></td>
</tr>
</table>
</form>
</form>
jquery 用于在单击输入时添加新行:
var i = $('table tr').length;
$(document).on('keyup', '.lst', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
html = '<tr>';
html += '<td>' + i + '</td>';
html += '<td><input type="text" class="inputs" name="serial_no' + i + '" id="serial_no' + i + '" /></td>';
html += '<td><input type="text" class="inputs lst" name="imei' + i + '" id="imei' + i + '" /></td>';
html += '</tr>';
$('table').append(html);
$(this).focus().select();
i++;
}
});
$(document).on('keydown', '.inputs', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
var index = $('.inputs').index(this) + 1;
$('.inputs').eq(index).focus();
event.preventDefault();
}
});
一件非常重要的事情要知道:这是一个动态的table,所以当按下回车键时,我实际上是在创建一个新行(见jquery功能)。
编辑:
打印 $_POST 给我这个:
Array ( [0] => serial1 ) Array ( [0] => imei1 ) serial2imei2serial3imei3serial4imei4
我是这样循环的:
$myArray = $_POST;
foreach ($myArray as $index) {
print_r($index);
}
编辑
准备好的语句:
$serial_no = e($_POST['serial_no']);
$imei = e($_POST['imei']);
$entrycount = count($serial_no);
$sales_date = date("'Y-m-d H:i:s'");
$cus_id = "1";
$stmt = $db->prepare("INSERT INTO devices (serial_imei,serial_no,type_id,cus_id,sales_date) VALUES (?,?,?,?,?)");
for ($loop = 1; $loop <= $entrycount; $loop++) {
$stmt->execute([$imei[$entrycount]], [$serial_no[$entrycount]], 1, $cus_id, $sales_date);
}
$pdo->commit();
您的表单将 post 2 个数组,其中包含您表单中 serial
和 imei
的值。在您确认它们具有相同数量的条目后,您只需遍历其中一个以从两个数组中提取值并将它们插入到您的数据库中。
由于您的问题不包含与数据库连接相关的任何内容,我只能向您展示将用于将数据插入数据库的变量。在进入循环之前使用 PDO 并准备插入(here's a good reference 让您使用 PDO 并准备)。
$serial_no = $_POST['serial_no'];
$imei = $_POST['imei'];
$entrycount = count($serial_no);
for ($loop = 1;$loop <= $entrycount; $loop++) {
//
// The values from your form will be in the following variables:
//
//
// $serial_no[$entrycount]
// $imei[$entrycount]
//
// The variables shown above are what you will have to insert into your DB.
//
}
我有一个包含一些文本输入的表格,我想在填写完这些表格后将其提交给数据库。我是 PHP 的新手,所以我不知道如何将所有值保存到数组中并提交。
这是我的 table 包装形式:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
</br>
<form action="" method="post">
<div id="wrapper">
<button type="submit" class="btn" name="add_device">Add devices</button>
</div>
</br>
<table id="ble_table" class="table table-bordered table-striped" cellspacing="0" width="100%">
<tr>
<th>No.</th>
<th>Serial no.</th>
<th>IMEI</th>
</tr>
<tr>
<td>1</td>
<td><input type="text" placeholder="Serial no." class="inputs" name="serial_no" id="serial_no" /></td>
<td><input type="text" placeholder="IMEI" class="inputs lst" name="imei" id="imei" /></td>
</tr>
</table>
</form>
</form>
jquery 用于在单击输入时添加新行:
var i = $('table tr').length;
$(document).on('keyup', '.lst', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
html = '<tr>';
html += '<td>' + i + '</td>';
html += '<td><input type="text" class="inputs" name="serial_no' + i + '" id="serial_no' + i + '" /></td>';
html += '<td><input type="text" class="inputs lst" name="imei' + i + '" id="imei' + i + '" /></td>';
html += '</tr>';
$('table').append(html);
$(this).focus().select();
i++;
}
});
$(document).on('keydown', '.inputs', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
var index = $('.inputs').index(this) + 1;
$('.inputs').eq(index).focus();
event.preventDefault();
}
});
一件非常重要的事情要知道:这是一个动态的table,所以当按下回车键时,我实际上是在创建一个新行(见jquery功能)。
编辑:
打印 $_POST 给我这个:
Array ( [0] => serial1 ) Array ( [0] => imei1 ) serial2imei2serial3imei3serial4imei4
我是这样循环的:
$myArray = $_POST;
foreach ($myArray as $index) {
print_r($index);
}
编辑 准备好的语句:
$serial_no = e($_POST['serial_no']);
$imei = e($_POST['imei']);
$entrycount = count($serial_no);
$sales_date = date("'Y-m-d H:i:s'");
$cus_id = "1";
$stmt = $db->prepare("INSERT INTO devices (serial_imei,serial_no,type_id,cus_id,sales_date) VALUES (?,?,?,?,?)");
for ($loop = 1; $loop <= $entrycount; $loop++) {
$stmt->execute([$imei[$entrycount]], [$serial_no[$entrycount]], 1, $cus_id, $sales_date);
}
$pdo->commit();
您的表单将 post 2 个数组,其中包含您表单中 serial
和 imei
的值。在您确认它们具有相同数量的条目后,您只需遍历其中一个以从两个数组中提取值并将它们插入到您的数据库中。
由于您的问题不包含与数据库连接相关的任何内容,我只能向您展示将用于将数据插入数据库的变量。在进入循环之前使用 PDO 并准备插入(here's a good reference 让您使用 PDO 并准备)。
$serial_no = $_POST['serial_no'];
$imei = $_POST['imei'];
$entrycount = count($serial_no);
for ($loop = 1;$loop <= $entrycount; $loop++) {
//
// The values from your form will be in the following variables:
//
//
// $serial_no[$entrycount]
// $imei[$entrycount]
//
// The variables shown above are what you will have to insert into your DB.
//
}