我正在使用预准备语句和for循环将多个产品插入表中..我遇到的问题是它只输入表单中的第一个产品。我有echo语句来测试for循环是否正常工作,并且所有正确的值都在我的页面上回显。为什么它不会将第一个产品插入到我的表中?
<?php
session_start();
//db info
$conn = new mysqli("$server","$user_name","$password","$database");
$productName = $_POST['productName'];
$productPrice = $_POST['productPrice'];
$productDescription = $_POST['productDescription'];
$productImage = $_POST['productImage'];
$id = $_SESSION['id'];
if (isset($productName) && isset($productPrice) && isset($productDescription) && isset($productImage)) {
$stmt = $conn->prepare("INSERT INTO Products (id, productNumber, productName, productPrice, productDescription, productImage)
VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("iissss", $id, $productNumber, $productNames, $productPrices, $productDescriptions, $productImages);
$productNameCount = count($productName);
for($i = 0; $i < $productNameCount; $i++) {
$productNumber = $i + 1;
echo $id." ";
echo $productNumber." ";
$productNames = $productName[$i];
echo $productNames." ";
$productPrices = $productPrice[$i];
echo $productPrices." ";
$productDescriptions = $productDescription[$i];
echo $productDescriptions." ";
$productImages = $productImage[$i];
echo $productImages."<br>";
$stmt->execute();
$stmt->reset();
}
$stmt->close();
}
mysqli_close($conn);
?>