dongxianghuan3587 2017-12-10 03: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-10 05: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 python使用pulp线性优化时报错
  • ¥15 开源或低价数据中台哪个最好
  • ¥15 arduino编程出现字符串疑似覆盖现象
  • ¥15 我的b站在没有碰到屏幕的情况下偶尔会自动跳出进度条,就像在屏幕上点了一下一样,但我并没有点。而且视频进度并没有变。这可能是什么原因造成的?
  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题