Codeigniter php - 更新数据库多行

Posted

技术标签:

【中文标题】Codeigniter php - 更新数据库多行【英文标题】:Codeigniter php - update the DataBase multi rows 【发布时间】:2020-06-11 07:08:20 【问题描述】:

我有网上商店项目,当有人点击“购买”时 -> 他购买了商品。 现在,我保存了该项目(它的工作)并想从购物车更新我的数据库(需要更新这些项目的数量)。

Controller - 查看函数 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.
    


型号 -

<?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;
        


数据库 - 需要更改:item_count

当我仅将一件商品插入购物车时,我成功更新了商品,但出现错误:

当我尝试更新多个项目时,只有第一个项目更新,我得到同样的错误。

谢谢,伊丹。

【问题讨论】:

【参考方案1】:

删除线

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

没有像mysql_query这样的Codeigniter功能,你为什么用这个?

【讨论】:

【参考方案2】:

Codeigniter 上没有 mysql_query 方法。您可以像这样删除它并更新模型代码:

    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.'"');
                
            
        
    

【讨论】:

很高兴我能帮上忙

以上是关于Codeigniter php - 更新数据库多行的主要内容,如果未能解决你的问题,请参考以下文章

codeigniter中的多个批量更新不起作用

使用 PHP 在 CodeIgniter 中使用用户输入的值更新数据库表

如何使用codeigniter 4框架在mysql数据库中插入多行?

在 PHP CodeIgniter foreach 循环中从数据库中提取和更新数据

如何使用 php 从多行表单更新 mysql 数据库 [关闭]

更新查询使 Codeigniter PHP 中 MySQL 的记录为空