我试图插入来自$ _POST表单的多个数组,但是即使尝试使用诸如“ while()”和“ foreach()”之类的方法,我也没有成功。这是我第一次使用数组执行此操作,所以我很迷茫。
我的代码是:
<input type='text' id="productName[]" name="productName[]">
<input type='text' id="quantity[]" name="quantity[]">
include_once("connection.php");
$name = $_POST['productName'];
$quantity = $_POST['quantity'];
$name = '"' . implode('","', $name) . '"';
$quantity = '"' . implode('","', $quantity) . '"';
$sql = "SELECT * FROM product WHERE name IN ($name)";
$result = mysqli_query($conn_app, $sql);
if (mysqli_affected_rows($conn_app) > 0){
while($row = mysqli_fetch_assoc($result)){
$itemId = $row['product_id'];
};
$insert = "INSERT INTO item_box (quantity,product_id) VALUES ('$quantity','$itemId')";
$result = mysqli_query($conn_app, $insert);
我希望结果为每个存在的数组插入行。喜欢:
"INSERT INTO item_box (quantity,product_id) VALUES ("20","3")";
"INSERT INTO item_box (quantity,product_id) VALUES ("50","5")";...
但我得到一个错误结果(许多数量值和一个product_id),例如:
"INSERT INTO item_box (quantity,product_id) VALUES ("20","30","40","50","2")"
噜噜哒