我尝试将多个值插入到来自数组的表行中。我几乎用这个答案解决了问题。Best way to INSERT many values in mysqli?
请检查接受的答案。在这个答案中,他是在一个列中添加值,但我希望它在多个列中。我如何做到这一点?
$array = array($u_id, $pid, $tradexx_price, $pvp, $product_title, $product_image);
$query = "INSERT INTO selection (c_id,p_id,tradexx_price,pvp,product_name,img) VALUES (?,?,?,?,?,?)";
$stmt = $con->prepare($query);
$stmt->bind_param("ssssss", $one);
$con->query("START TRANSACTION");
foreach ($array as $one) {
$stmt->execute();
}
$stmt->close();
$con->query("COMMIT");
但这段代码给了我一个错误。
Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in
如有任何问题,欢迎回答。
完整脚本代码
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include '../includes/conn.php';
if (isset($_POST['insert_selection'])) {
$pid = implode(',', $_POST['id']);
$u_id = $_POST['u_id'];
foreach ((array)$pid as $p) {
$get_pro = "SELECT * FROM products WHERE id IN ($p)";
$run_pro = mysqli_query($con, $get_pro);
while ($row_pro = mysqli_fetch_array($run_pro)) {
$id = $row_pro['id'];
$product_title = $row_pro['product_title'];
$tradexx_price = $row_pro['product_price'];
$pvp = $row_pro['pvp'];
$product_image = $row_pro['product_image'];
$array = array($u_id, $id, $tradexx_price, $pvp, $product_title, $product_image);
$query = "INSERT INTO selection (c_id,p_id,tradexx_price,pvp,product_name,img) VALUES (?,?,?,?,?,?)";
$stmt = $con->prepare($query);
$stmt->bind_param("ssssss", $cur_c_id, $cur_id, $cur_tradexx_price, $cur_pvp, $cur_product_name, $cur_img);
$con->query("START TRANSACTION");
foreach ($u_id as $i => $cur_c_id) {
$cur_id = $id[$i];
$cur_tradexx_price = $tradexx_price[$i];
$cur_pvp = $pvp[$i];
$cur_product_name = $product_title[$i];
$cur_img = $product_image[$i];
$stmt->execute();
}
$stmt->close();
$con->query("COMMIT");
}
}
}$pid
是复选框中的值
1条答案
按热度按时间wwtsj6pe1#
您不需要循环。您可以使用
SELECT
查询作为INSERT
的数据源。