我有两个jquery动态输入。我如何插入数据库所有项目。像这样;
fatura | urun
1 apple
2 banana
这里是Html代码;
<form action="post.php" id="form" method="post">
<input name="fatura[]" type="text" />
<input name="urun[]" type="text" " />
</div>
</div>
<button class="btn btn-primary input-ekle">Add Item</button>
<input type="submit" value="Send">
</form>
这里是post.php; (这个代码只添加“fatura []”输入行。我想插入urun输入。)
$sql = "INSERT INTO siparis (fatura_id) VALUES (:fi)";
$ekle = $db->prepare($sql);
$ekle->bindParam(':fi', $row);
foreach ($_POST['fatura'] as $row) {
$ekle->execute();
}
if ($ekle) {
echo "ok";
} else {
echo "bad";
}
答案 0 :(得分:0)
假设有多少&#34; fatura&#34; as&#34; urun&#34;,您可以尝试这样的事情:
$sql = "INSERT INTO siparis (fatura_id, urun_ismi) VALUES (:fi, :ui)" ;
$ekle = $db->prepare($sql) ;
$num = count($_POST['fatura']) ;
for ($i = 0 ; $i < $num ; $i++) {
$ekle->bindParam(':fi', $_POST['fatura'][$i]) ;
$ekle->bindParam(':ui', $_POST['urun'][$i]) ;
$ekle->execute();
}