如何使用php mysql一次插入多个查询?

时间:2018-10-22 17:11:57

标签: php mysqli

如何一次执行多个查询。这是我的PHP代码。

if (isset($_POST["getexpense_amount"])) {
    $expense_typeDate = $_POST['getexpense_typeDate'];
    $expense_type     = mysqli_real_escape_string($link, $_POST['getexpense_type']);
    $expense_doneby   = mysqli_real_escape_string($link, $_POST['getexpense_doneby']);
    $expense_amount   = $_POST['getexpense_amount'];
    $getmodeofexpense = $_POST['getmodeofexpense'];
    date_default_timezone_set('Asia/Dubai');
    //$created        = "2018-10-16 16:39:18";
    $created = date("y-m-d H:i:s");

    $sql1                    = "select date from moneybox order by ID desc limit 1";
    $result1                 = mysqli_query($link, $sql1);
    $row1                    = mysqli_fetch_array($result1);
    $last_money_created_date = $row1['date'];

    //To get the current Opening balance of cash
    $sqlclosebalcash     = "SELECT closing_balance FROM `moneybox` WHERE `type`='cash' ORDER BY ID DESC LIMIT 1";
    $resultclosebal_cash = mysqli_query($link, $sqlclosebalcash);
    $rowclosebal_cash    = mysqli_fetch_array($resultclosebal_cash);

    //To get the current Opening balance of card
    $sqlclosebalcard     = "SELECT closing_balance FROM `moneybox` WHERE `type`='bank' ORDER BY ID DESC LIMIT 1";
    $resultclosebal_card = mysqli_query($link, $sqlclosebalcard);
    $rowclosebal_card    = mysqli_fetch_array($resultclosebal_card);

    $tz        = 'Asia/Dubai'; // your required location time zone.
    $timestamp = time();
    $dt        = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
    $dt->setTimestamp($timestamp); //adjust the object to correct timestamp
    $todayDate = $dt->format('Y-m-d'); //returns 2018-10-21

    if ($rowclosebal_cash['closing_balance'] == '') {
        $last_moneybox_closingbalanacecash = "0.00";
    } else {
        $last_moneybox_closingbalanacecash = $rowclosebal_cash['closing_balance'];
    }
    if ($rowclosebal_card['closing_balance'] == '') {
        $last_moneybox_closingbalanacecard = "0.00";
    } else {
        $last_moneybox_closingbalanacecard = $rowclosebal_card['closing_balance'];
    }



    $query .= "INSERT INTO expense_details (date,expense_type,doneby,amount,expense_mode,created_at)
                        VALUES ('$expense_typeDate', '$expense_type', '$expense_doneby', $expense_amount, '$getmodeofexpense','$created');";

    $cal_closingbalancecash = $last_moneybox_closingbalanacecash - $expense_amount;
    $cal_closingbalancecard = $last_moneybox_closingbalanacecard - $expense_amount;



    // check whether card or cash or both rows are already there or not
    $sql1       = "select * from moneybox WHERE date='$todayDate' AND type='cash' ";
    $resultcash = mysqli_query($link, $sql1);
    if (mysqli_num_rows($resultcash)) {
        $cashMode = 1;
    } else {
        $cashMode = 0;
    }

    $sql1       = "select * from moneybox WHERE date='$todayDate' AND type='bank' ";
    $resultcard = mysqli_query($link, $sql1);
    if (mysqli_num_rows($resultcard)) {
        $cardMode = 1;
    } else {
        $cardMode = 0;
    }



    switch ($getmodeofexpense) {
        case "cash":
            if ($cashMode === 0) {
                //echo 'Different Date cash'; //insert happen based on the type
                $last_moneybox_created_date = $todayDate;
                $cashMode                   = 1;

                $query = "INSERT INTO moneybox (type,opening_balance,inflow,outflow,date,closing_balance,actual_closing_balance) 
                                VALUES ('cash','$last_moneybox_closingbalanacecash','0.00','$expense_amount','$todayDate','$cal_closingbalancecash','0.00');";
            } else {
               // echo 'Same Date cash'; //update happen based on type and date
                $query = "UPDATE moneybox SET 
                                        outflow = outflow + $expense_amount, 
                                        closing_balance= opening_balance + inflow - outflow 
                                        WHERE type = 'cash' and date = '$todayDate';";
            }
            break;

        case "card":
            if ($cardMode === 0) {
                //echo 'Different Date card'; //insert happen based on the type
                $last_moneybox_created_date = $todayDate;
                $cardMode                   = 1;

                $query = "INSERT INTO moneybox (type,opening_balance,inflow,outflow,date,closing_balance,actual_closing_balance) 
                                VALUES ('bank','$last_moneybox_closingbalanacecard','0.00','$expense_amount','$todayDate','$cal_closingbalancecard','0.00');";
            } else {
               // echo 'Same Date card'; //update happen based on type and date
                $query = "UPDATE moneybox SET 
                                            outflow = outflow + $expense_amount, 
                                            closing_balance= opening_balance +     inflow - outflow 
                                            WHERE type = 'bank' and date = '$todayDate';";
            }
            break;

    } // end switch case

    if (mysqli_query($link, $query)) {

        echo "Expense Details inserted successfully";
    } else {
        echo "0";
        //echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}

在这里,第一次插入在for循环中无法正常工作。其他插入和更新都可以正常工作。

$query .= "INSERT INTO expense_details (date,expense_type,doneby,amount,expense_mode,created_at)
                        VALUES ('$expense_typeDate', '$expense_type', '$expense_doneby', $expense_amount, '$getmodeofexpense','$created');";

以上插入操作没有执行。我可以一键执行所有这些查询。如何解决此问题。

0 个答案:

没有答案