dongxianghuan3587 2017-12-09 19:55
浏览 57
已采纳

Codeigniter中的标准插入查询

I am just new in CodeIgniter. After executing the program, every time I submit the form I'm always receiving this (in the picture)php error

Heres my code...

item_model.php (model)

class Item_model extends CI_Model
{

    public function insertItem()
    {
        $code        = $this->input->post('prod_code');
        $name        = $this->input->post('prod_name');
        $category    = $this->input->post('category');
        $price       = $this->input->post('price');

        $sql = "INSERT INTO `system`.`products` (`prod_code`,`prod_name`,`category_id`,`original_price`) VALUES ("
            .$this->db->escapeString($code).", ".$this->db->escapeString($name)
            .",(select category_id from category where category_name = "
            .$this->db->escapeString($category)."), "
            .$this->db->escapeString($price).")";

        $result = $this->db->query($sql);
        if($this->db->affected_row() === 1)
        {
            $this->session->set_flashdata('successMessage',
                                 '<div class="alert alert-success">New product has been registered</div>');
            redirect(base_url('new_item'));
        }
    }
}

item_con.php (controller)

public function item_con()
{
    $this->form_validation->set_rules('prod_name', 'Product Name', 'required|min_length[3]');
    $this->form_validation->set_rules('price', 'Original Price', 'required|integer');
    if($this->form_validation->run() == FALSE)
    {
        $this->session->set_flashdata('errorMessage', '<div class="alert alert-danger">'.validation_errors().'</div>');
        redirect(base_url('new_item'));
    }
    else
    {
        $this->load->model('item_model');
        $result = $this->item_model->insertItem();
        if($result)
        {
            $this->session->set_flashdata('successMessage',
               '<div class="alert alert-success">New product has been registered</div>');
            redirect(base_url('new_item'));
        }
    }
}

I just want to insert the product details in the products table. The category_id is foreign key in product table. And I want that everytime I insert the product details, it reads the category_id of the product from category table. I hope you could help. Thanks

展开全部

  • 写回答

1条回答 默认 最新

  • drv13270 2017-12-09 21:52
    关注

    The error is telling you that PHP cannot figure out what $db is.

    Have you loaded the database? Probably not.

    You can do it in the model with

    $this->load->database();
    

    See documentation

    Or it can be "autoloaded" (See documentation) with

    $autoload['libraries'] = array('database');
    

    Loading a database only works if it is configured properly too. (Docs)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了