php 遇到未捕获的异常错误

lc8prwob  于 2023-01-29  发布在  PHP
关注(0)|答案(1)|浏览(146)
遇到未捕获的异常

类型:错误
消息:调用bool上的成员函数num_rows()
文件名:C:\xampp\htdocs\sikan_v2\应用程序\视图\交易\销售\购物车数据. php
行号:2
回溯:
文件:C:\应用程序\视图\交易\销售\销售表单. php
生产线:134
功能:查看
文件:C:\应用程序\库\模板. php
生产线:14
功能:查看
文件:C:\xampp\htdocs\sikan_v2\应用程序\控制器\销售. php
生产线:26
功能:加载
文件:C:\文件夹
生产线:315
函数:需要一次
我的控制器:controllers/sale.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Sale extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        check_not_login();
        check_admin();
        $this->load->model(['sale_m','item_m', 'supplier_m', 'stock_m']);
    
    } 

    public function index()
    {
        $this->load->model(['customer_m', 'item_m']);
        $customer = $this->customer_m->get()->result();
        $item = $this->item_m->get()->result();
        $cart = $this->sale_m->get_cart();
        $data = array(
            'customer' => $customer,
            'item' => $item,
            'cart' => $cart,
            'invoice' => $this->sale_m->invoice_no(),
        );
        $this->template->load('template', 'transaction/sale/sale_form', $data);
    }

    public function process()
    {
        $data = $this->input->post(null, TRUE);

        if(isset($_POST['add_cart'])) {
            $this->sale_m->add_cart($data);
        }

        if($this->db->affected_rows() > 0) {
            $params = array("success" => true);
        } else {
            $params = array("success" => false);
        }
        echo json_encode($params);
    }

}

我的模特:models/sale_m.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Sale_m extends CI_Model {

    public function invoice_no()
    {
        $sql = "SELECT MAX(MID(invoice,9,4)) AS  invoice_no
            FROM t_sale
            WHERE MID(invoice,3,6) = DATE_FORMAT(CURDATE(), '%y%m%d')";
        $query = $this->db->query($sql);
        IF($query->num_rows() > 0) {
            $row = $query->row();
            $n = ((int)$row->invoice_no) + 1;
            $no = sprintf("%'.04d", $n);
        }else{
            $no = "0001";
        }
        $invoice = "MP".date('ymd').$no;
        return $invoice;
        $query = $this->db->get();
        return $query;
    }

    public function get_cart($params = null)
    {
        $this->db->select('*, p_item.name as item_name, t_cart.price as cart_price');
        $this->db->from('cart');
        $this->db->join('p_item', 't_cart.item_id = p_item.item_id');
        if($params != null) {
            $this->db->where($params);
        }
        $this->db->where('user_id', $this->session->userdata('userid'));
        $query = $this->db->get();
        return $query;
    }


    public function add_cart($post) 
    {
        $query = $this->db->query("SELECT MAX(cart_id) AS cart_no FROM t_cart");
        if($query->num_rows() > 0) {
            $row = $query->row();
            $car_no = ((int)$row->cart_no) + 1;
        } else {
            $car_no = "1";
        }

        $params = array(
            'cart_id' => $car_no,
            'item_id' => $post['item_id'],
            'price' => $post['price'],
            'qty' => $post['qty'],
            'total' => ($post['price'] * $post['qty']),
            'user_id' => $this->session->userdata('userid')
        );
        $this->db->insert('t_cart', $params);
    }

    

}

我的观点:/交易/购物车_data. php

<?php $no = 1;
if($cart->num_rows() > 0) {
    foreach ($cart->result() as $c =>$data) { ?>
        <tr>
            <td><?=$no++?>.</td>
            <td><?=$data->barcode?></td>
            <td><?=$data->item_name?></td>
            <td class="text-right"><?=$data->cart_price?></td>
            <td class="text-center"><?=$data->qty?></td>
            <td class="text-right"><?=$data->discount_item?></td>
            <td class="text-right"id="total"><?=$data->total?></td>
            <td class="text-center" width="160px">
            <button id="update_cart" data-toggle="modal" data-target="#modal-item-edit"
            data-cartid="<?=$data->cart_id?>"
            data-barcode="<?=$data->barcode?>"
            data-product="<?=$data->item_name?>"
            data-price="<?=$data->cart_price?>"
            data-qty="<?=$data->qty?>"
            data-discount="<?=$data->discount_item?>"
            data-total="<?=$data->total?>"
            class="btn btn-xs btn primary">
            <i class="fa fa-pencil"></i> Update
            </button>
            <button id="del_cart" data-cartid="<?=$data->cart_id?>" class="btn btn-xs btn-danger">
                <i class="fa fa-trash"></i> Delete
            </button>
            </td>
        </tr>
    <?php
    }
} else { 
    echo '<tr>
    <td colspan="8"> class="text-center">Tidak ada item</td>
    </tr>';
} ?>

请帮我解决这个错误

wkyowqbh

wkyowqbh1#

当你的查询出现错误时,它会返回bool,再次检查这个函数:

public function get_cart($params = null)
{
    $this->db->select('*, p_item.name as item_name, t_cart.price as cart_price');
    $this->db->from('cart');
    $this->db->join('p_item', 't_cart.item_id = p_item.item_id');
    if($params != null) {
        $this->db->where($params);
    }
    $this->db->where('user_id', $this->session->userdata('userid'));
    $query = $this->db->get();
    return $query;
}

我想应该是:

$this->db->from('t_cart');

如果再次检查没有发现错误,可以使用方法:last_query获取查询,然后尝试直接在phpmyadmin或MYSQL Workbench中查询,检查是否有错误。

public function get_cart($params = null)
{
    // above source code
    $query = $this->db->get();
    return $this->db->last_query();
}

相关问题