在foreach循环中插入数据库,同时仍然在序列中调用不同的函数

时间:2018-06-05 12:30:34

标签: php ajax

我有一点错误,我无法分辨出错误的来源。

我正在构建一个购物车系统,该系统将在提交时从购物车中将多个数据插入数据库。

我正在使用AJAX而且我有错误,请我帮忙。

以下是我的代码段:

JavaScript代码

function addSale(payment_type, cash_tendered) {
var cust_name = $("#cust_name").val();
var amt_owed = $("#amt_owed").val();

$.confirm({
    title: 'Checkout',
    content: '' +
            '<form action="" class="formName" role="form">' +
            '<div class="form-group">' +
            '<label>Payment Type</label>' +
            '<select id="eType" class="name form-control">' +
            '<option value="cash">Cash</option>' +
            '<option value="card">Card</option>' +
            '</select>' +
            '</div>' +
            '<div class="form-group">' +
            '<label>Cash Tendered</label>' +
            '<input type="number" id="eCash" placeholder="Cash Tendered" class="name form-control">' +
            '</div>' +
            '</form>',
    buttons: {
        cancel: function () {
            //close
        },
        formSubmit: {
            text: 'Checkout',
            btnClass: 'btn-success',
            action: function () {
                payment_type = this.$content.find('#eType').val();
                cash_tendered = this.$content.find('#eCash').val();


                if (!payment_type || !cash_tendered) {
                    $.alert('Please fill all fields.');
                    return false;
                }

                $.confirm({
                    title: 'Do you want to continue?',
                    type: 'orange',
                    content: 'Click Ok to add sale',
                    buttons: {
                        cancel: function () {
                        },
                        proceed: {
                            text: 'Ok',
                            btnClass: 'btn btn-success',
                            action: function () {
                                var addUrl = "home/addsales";
                                addUrl += "/" + payment_type;
                                addUrl += "/" + cash_tendered;
                                addUrl += "/" + cust_name;
                                addUrl += "/" + amt_owed;

                                //
                                $.ajax({type: 'GET', url: addUrl, data: {},
                                    success: function (result) {
                                        $.alert({
                                            content: result
                                        });
                                        $("#eType").val("");
                                        $("#eCash").val("");
                                        $("#cust_name").val("");
                                        $("#amt_owed").val("");
                                        location.reload();
                                    },
                                    error: function (xhr, status, error) {

                                        $.alert({
                                            content: 'Could not complete the process. ' + error
                                        });
                                    }
                                });
                            }
                        }
                    }
                });
            }
        }
    },
    onContentReady: function () {
        // bind to events
        var jc = this;
        this.$content.find('form').on('submit', function (e) {
            // if the user submits the form by pressing enter in the field.
            e.preventDefault();
            jc.$$formSubmit.trigger('click'); // reference the button and click it
        });
    }
});

}

以下是家庭控制器代码:

private function addsales($payment_type = null, $cash_tendered = null, $cust_name = null, $amt_owed = null) {
    if (isset($payment_type, $cash_tendered)) {
        $email = $_SESSION[DbStrings::$EMAIL];
        $payment_type = $this->test_input($payment_type);
        $cash_tendered = $this->test_input($cash_tendered);


        $insertedSale = $this->member->insertDailySale($email, $payment_type, $cash_tendered);
        $cust_name = $this->test_input($cust_name);
        $amt_owed = $this->test_input($amt_owed);
        $insertedCredit = 1;
        if (isset($cust_name, $amt_owed) && $amt_owed > 0) {
            $insertedCredit = $this->member->insertCredit($email, $cust_name, $amt_owed);
        }

        if ($insertedSale && $insertedCredit) {

            $_SESSION['temp_invoice'] = $_SESSION[DbStrings::$INVOICE];

            $chars = "003232303232023232023456789";
            srand((double) microtime() * 1000000);
            $i = 0;
            $pass = '';
            while ($i <= 7) {

                $num = rand() % 33;

                $tmp = substr($chars, $num, 1);

                $pass = $pass . $tmp;

                $i++;
            }


            $alpha = 'NM-' . $pass;
            $_SESSION[DbStrings::$INVOICE] = $alpha;
            echo "Your sale has been inserted succesfully";
        } else {
            echo "There was a problem inserting your sale. Please try again.";
        }
    } else {
        echo 'Please fill all fields';
    }
}

以下是我的模型代码,它仍然提取其他功能:

public function insertDailySale($email, $payment_type, $cash_tendered) {

    $invoice = $_SESSION[DbStrings::$INVOICE];
    $this->db->from(DbStrings::$SALES_ORDER_TABLE_NAME);
    $condition = array(DbStrings::$EMAIL => $email, DbStrings::$INVOICE => $invoice);
    $this->db->where($condition);
    $query = $this->db->get();
    $checks = $query->result_array();

    foreach ($checks as $queries) {
        $productID = $queries[DbStrings::$PRODUCTID];
        $quantity = $queries[DbStrings::$SALES_QUANTITY];
        $amount = $queries[DbStrings::$SALES_AMOUNT];
        $profit = $queries[DbStrings::$SALES_PROFIT];
        $product_code = $queries[DbStrings::$PRODUCT_CODE];
        $product_name = $queries[DbStrings::$PRODUCT_NAME];
        $product_selling = $queries[DbStrings::$PRODUCT_SELLING];

        $this->deductInventory($email, $product_code, $quantity);
        $this->updateQuantitySold($email, $product_code, $quantity);
        $cost_price = $this->product->getCostPrice($product_code);

        $data[] = array(
            DbStrings::$EMAIL => $email,
            DbStrings::$INVOICE => $invoice,
            DbStrings::$PRODUCTID => $productID,
            DbStrings::$SALES_QUANTITY => $quantity,
            DbStrings::$SALES_AMOUNT => $amount,
            DbStrings::$SALES_PROFIT => $profit,
            DbStrings::$PRODUCT_CODE => $product_code,
            DbStrings::$PRODUCT_NAME => $product_name,
            DbStrings::$PRODUCT_CP => $cost_price,
            DbStrings::$PRODUCT_SP => $product_selling,
            DbStrings::$PAYMENT_TYPE => $payment_type,
            DbStrings::$CASH_TENDERED => $cash_tendered,
            DbStrings::$DATE_CREATED => time()
        );
        $inserted = $this->db->insert_batch(DbStrings::$DAILYSALES_TABLE_NAME, $data);
    }

    return $inserted;
}

通过上述内容,我得到了这个错误的标记:

  

插入促销时出现问题。请再试一次。

我需要帮助。

1 个答案:

答案 0 :(得分:0)

错误来自我的数据库结构。作为internal server error,它来自数据库。

我意识到我从数据库表中删除了一个列,并且我仍在将数据发送到已删除的列。

感谢各位帮忙的尝试。