在for循环mysql内部运行multiquery

时间:2018-09-24 11:00:04

标签: ajax serialization

我正在尝试在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中的一行。

1 个答案:

答案 0 :(得分:0)

尝试

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之外运行查询