我有文本框数组
<form method="POST">
<input type="text" name="partnumber[]" value="1234">
<input type="text" name="desc[]" value="used">
<input type="text" name="qty[]" value="24">
<input type="text" name="partnumber[]" value="2345">
<input type="text" name="desc[]" value="good">
<input type="text" name="qty[]" value="31">
<input type="text" name="partnumber[]" value="3456">
<input type="text" name="desc[]" value="brand new">
<input type="text" name="qty[]" value="22">
<input type="submit">
</form>
发布后,我想这样插入我的数据库。具有相同的ref_id。
last_insert_id = 17;
id | ref_id | partnumber| description | quantity
1 | 17 | 1234 | used | 24
2 | 17 | 2345 | good | 31
3 | 17 | 3456 | brand new | 22
任何帮助都会得到帮助。谢谢..
$comma = "";
$values = "";
$last_id = 17;
foreach ($partnumber as $p) {
foreach ($desc as $d) {
foreach ($qty as $q) {
}
}
$values .= $comma."($id,$last_id,'$p','$d','$q')";
$comma = ",";
}
$sql = "INSERT INTO parts_replaced (id,last_id,partnumber,description,quantity) VALUES $values";
最佳答案
您可以像这样摆脱这种形式。
<form method="POST">
<input type="text" name="partnumber[]" value="1234">
<input type="text" name="desc[]" value="used">
<input type="text" name="qty[]" value="24">
<input type="text" name="partnumber[]" value="2345">
<input type="text" name="desc[]" value="good">
<input type="text" name="qty[]" value="31">
<input type="text" name="partnumber[]" value="3456">
<input type="text" name="desc[]" value="brand new">
<input type="text" name="qty[]" value="22">
<input type="submit">
</form>
<?php
if(isset($_POST['partnumber']))
{
$parnumber=$_POST['partnumber'];
foreach ($parnumber as $key => $value) {
echo $value.'-'.$_POST['desc'][$key].'-'.$_POST['qty'][$key].'<br />';
//add your query here
}
}
?>
关于php - PHP多个文本框数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32471668/