简体   繁体   English

Codeigniter php - 更新数据库多行

[英]Codeigniter php - update the DataBase multi rows

I have online shop project, when someone click "purchase" -> he buy item.我有在线商店项目,当有人点击“购买”-> 他购买商品时。 Now, i save the item (it's work) and want to update my Database (need to update the amount of those item's) from the cart.现在,我保存了该项目(它正在工作)并想从购物车更新我的数据库(需要更新这些项目的数量)。

Controller - look at function insertOrderDataController and indise it the line $updateCount = $this->product_model->updateItemCount($data,$counter);控制器 -查看函数insertOrderDataController并将其显示为$updateCount = $this->product_model->updateItemCount($data,$counter);

<?php

class webs_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('login_model');
        $this->load->model('static_model');
        $this->load->model('product_model');
        $this->load->helper('url_helper');
        $this->load->helper('form');
        $this->load->library('session');
    }

    public function view($page = 'home') {
        if (!file_exists(APPPATH . 'views/pages/' . $page . '.php')) {
            // Whoops, we don't have a page for that!
            show_404();
        }
        $data['title'] = ucfirst($page); // Capitalize the first letter
        $this->load->view('templates/header', $data);
        $this->load->view('pages/' . $page, $data);
        $this->load->view('templates/footer', $data);
    }

    public function home() {
        $data['user'] = $this->session->all_userdata();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/home', $data);
        $this->load->view('templates/footer');
    }

    public function products() {
        $data['user'] = $this->session->all_userdata();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/our_products', $data);
        $this->load->view('templates/footer');
    }

    public function contact() {
        $data['user'] = $this->session->all_userdata();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/contact', $data);
        $this->load->view('templates/footer');
    }

    public function our_products() {

        $data['title'] = 'Produts';
        $data['user'] = $this->session->all_userdata();
        $totalBill['total'] = $this->product_model->CalculateTotalBill($data);
        $data['product'] = $this->product_model->get_product();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/our_products', $data);
        $this->load->view('templates/footer');
    }

    public function calcit() {
        $data['user'] = $this->session->all_userdata();
        $data['calc'] = $this->product_model->CalculateTotalBill($data);
    }

    public function google_pie_chart() {
        $data['title'] = 'statistic view';
        $data['user'] = $this->session->all_userdata();
        $data['year_pie'] = $this->static_model->get_Stat();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/google_pie_chart', $data);
        $this->load->view('templates/footer');
    }

    public function check($data, $counterCart) {

        $error = '';
        $data['productDB'] = $this->product_model->get_product();
        $lengthDB = count($data['productDB']);

        for ($i = 0; $i < $counterCart; $i++) {
            $serialCartItem = intval($data[$i]['serial_number']);
            $countCartItem = intval($data[$i]['count_purchase']);

            for ($y = 0; $y < $lengthDB; $y++) {
                $serialDBItem = intval($data['productDB'][$y]['serial_number']);
                $countDBItem = intval($data['productDB'][$y]['item_count']);

                if ($serialDBItem == $serialCartItem) {

                    if ($countDBItem <= $countCartItem) {
                        $error.= "Not enough of this product in stock ! You will move to Homepage.";
                    }
                }
            }
        }
        return $error;
    }

    public function insertOrderDataController() {
        $counter = $this->input->post('counter');
        $dataUser['user'] = $this->session->all_userdata();
        $TheUsername = $dataUser['user']['User_name'];

        for ($i = 0; $i < $counter; $i++) {
            $data[$i] = array(
                'User_name' => $TheUsername,
                'serial_number' => $this->input->post('serial_number[' . $i . ']'),
                'count_purchase' => $this->input->post('count_purchase[' . $i . ']')
            );
        }

        $error = $this->check($data, $counter);
        if ($error == '') {
            $error_db = $this->product_model->insertOrderData($data);
            if ($error_db == NULL) {
                $updateCount = $this->product_model->updateItemCount($data,$counter);
                $data['info'] = array("message" => "1");
                $messageAlert = 'Thanks you for purchase !';
                echo "<script type='text/javascript'>alert('$messageAlert');</script>";
//                $updateCount = $this->product_model->updateItemCount($data,$counter);
            } else {
                $data['info'] = array("message" => "Error. Registration faild: " . $error_db["message"]);
            }
        } else {
            echo "<script type='text/javascript'>alert('$error');</script>";
        }
        redirect("webs_controller/home");  //if activ - there is not alert.
    }

}

model -模型 -

<?php

class product_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function get_product() {
        $query = $this->db->query('SELECT * FROM `product`');
        return $query->result_array();
    }

    public function CalculateTotalBill($username) {
        $user= $username['user']['User_name'];
        $query = $this->db->query("SELECT bought_history.serial_number, bought_history.count_purchase, product.item_price FROM bought_history INNER JOIN product ON bought_history.serial_number=product.serial_number WHERE User_name IN (select User_name FROM bought_history WHERE User_name='$user')");
        $temp = $query->result_array();
        $total = 0;
        for ($i = 0; $i < count($temp); $i++) {
            $total = $total + ($temp[$i]['count_purchase'] * $temp[$i]['item_price']);
        }
        echo "The current total pay : <b>",$total,"$</b>";
        return $total;
    }

    public function updateItemCount($data,$counterCart){
       $data['productDB'] = $this->product_model->get_product();
        $lengthDB = count($data['productDB']);

        for ($i = 0; $i < $counterCart; $i++) {
            $serialCartItem = intval($data[$i]['serial_number']);
            $countCartItem = intval($data[$i]['count_purchase']);

            for ($y = 0; $y < $lengthDB; $y++) {
                $serialDBItem = intval($data['productDB'][$y]['serial_number']);
                $countDBItem = intval($data['productDB'][$y]['item_count']);

                if ($serialDBItem == $serialCartItem) {
                    $updatedCalc = $countDBItem-$countCartItem;
                    $query = $this->db->query('UPDATE `product` SET `item_count` = "'.$updatedCalc.'" WHERE `product`.`serial_number` = "'.$serialDBItem.'"');
                    $update = $this->db->mysql_query($query);
                    }
                }
            }
        }


     public function insertOrderData($data){ 
            $this->db->db_debug = FALSE; 

             $error=NULL;
              if (!$this->db->insert_batch('bought_history', $data)){
                  $error=$this->db->error();
              }
              return $error;
        }

}

Database - need to be change : item_count数据库 -需要更改: item_count

在此处输入图片说明

I succsess update the item when i insert only one item to cart, but i get error :当我只将一件商品插入购物车时,我成功更新了商品,但出现错误: 在此处输入图片说明

when i try update more then one item, only the first item update, and i get the same error .当我尝试更新多个项目时,仅更新第一个项目,并且出现相同的错误。

Thanks, Idan.谢谢,伊丹。

Remove the line删除该行

$update = $this->db->mysql_query($query);

There is no Codeigniter function like mysql_query , why are you using this?没有像mysql_query这样的 Codeigniter 函数,你为什么要使用它?

There is no mysql_query method on Codeigniter. mysql_query上没有mysql_query方法。 You could remove it and update the model codes like this :您可以删除它并更新模型代码,如下所示:

    public function updateItemCount($data,$counterCart){
        $data['productDB'] = $this->product_model->get_product();
        $lengthDB = count($data['productDB']);

        for ($i = 0; $i < $counterCart; $i++) {
            $serialCartItem = intval($data[$i]['serial_number']);
            $countCartItem = intval($data[$i]['count_purchase']);

            for ($y = 0; $y < $lengthDB; $y++) {
                $serialDBItem = intval($data['productDB'][$y]['serial_number']);
                $countDBItem = intval($data['productDB'][$y]['item_count']);

                if ($serialDBItem == $serialCartItem) {
                    $updatedCalc = $countDBItem-$countCartItem;
                    $this->db->query('UPDATE `product` SET `item_count` = "'.$updatedCalc.'" WHERE `product`.`serial_number` = "'.$serialDBItem.'"');
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM