在内部为循环 MySQL 运行多查询



我正在尝试在 for 循环中运行多查询。帖子变量来自动态表

if ($resultset->num_rows == 0) {
for ($i = 0; $i < count($_POST["savi_account_number"]); $i++) {
$insert = "INSERT INTO `savings_transactions_table` (`id`, `receipt_id`, `sub_code`, `savings_account`, `user_id`, `amount`, `dr`, `cr`, `actual_balance`, `available_balance`, `created_user`, `created_date`)
VALUES (NULL, '$receipt_id', '" . $_POST['savings_product'][$i] . "', '" . $_POST['savi_account_number'][$i] . "', '$member_id', '" . $_POST['savi_dep_amount'][$i] . "', '0', '" . $_POST['savi_dep_amount'][$i] . "', '0', '0', '$user_id', '$create_date');";
$insert.="INSERT INTO `gl_transactions_table` (`id`, `member_id`, `posting_product`, `product_account`, `gl_account`, `dr`, `cr`, `trx_code`, `trx_type`, `trx_reference`, `posted_date`, `posted_by`)
VALUES (NULL, '$member_id', '" . $_POST['savings_product'][$i] . "', '" . $_POST['savi_account_number'][$i] . "', '" . $_POST['savings_cap_gl'][$i] . "', '0', '" . $_POST['savi_dep_amount'][$i] . "', '$deposit_type', '$transaction_type', '$transaction_reference', '$create_date', '$user_id')";
$res = mysqli_multi_query($conn, $insert);}

但查询不会插入所有值,而只会插入 DB 中的一行。

试试这个

if ($resultset->num_rows == 0) {
for ($i = 0; $i < count($_POST["savi_account_number"]); $i++) 
{
$insert .= "INSERT INTO `savings_transactions_table` (`id`, `receipt_id`, `sub_code`, `savings_account`, `user_id`, `amount`, `dr`, `cr`, `actual_balance`, `available_balance`, `created_user`, `created_date`)
VALUES (NULL, '$receipt_id', '" . $_POST['savings_product'][$i] . "', '" . $_POST['savi_account_number'][$i] . "', '$member_id', '" . $_POST['savi_dep_amount'][$i] . "', '0', '" . $_POST['savi_dep_amount'][$i] . "', '0', '0', '$user_id', '$create_date');";
}
$res = mysqli_multi_query($conn, $insert);

for loop外运行查询

最新更新