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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看