将数据插入数据库

时间:2015-07-21 21:35:18

标签: php codeigniter

它是我的控制器代码`

class Booking_Controller extends CI_Controller{

    public function __construct(){

        parent::__construct();
        $this->load->model('Books_model');

    }

public function view()
{


    $data['result']=$this->Books_model->get_restaurants();

    $this->load->helper(array('form','url'));
    $this->load->view('restaurants/booking',$data);

    $this->Books_model->insert_into_db();

}


}

Books_model`code是

<?php

class Books_model extends CI_Model{

public function __construct(){

    $this->load->database();

}

public function get_restaurants()
{
    $sql = "SELECT restaurant_id, names FROM restaurants ";
    $query = $this->db->query( $sql );
    return $query->result();

}

public function insert_into_db()
{
   $people =$this->input->post('No.of. People');
   return $this->db->insert('reservation',$people);

}

}

查看代码

<form action="<?php echo base_url();?>Booking_Controller/view" method="post">

                    <table>
                        <tr>
                            <td>number of people</td>
                    <td>number of people = <input type = 'text' name='No.of. People'></td>
                        </tr>
                    <tr>
                        <td><input type='submit' value="Submit"></td>

                    </tr>
                </table>
                </form>

问题是,当我运行项目时,它会给我这个错误...

发生数据库错误 你必须使用&#34; set&#34;更新条目的方法。

plz指导我错误的地方

`

1 个答案:

答案 0 :(得分:0)

根据CodeIgniter文档,insert命令的第二个参数应该是数组(或对象),而不是字符串。

因此,如果您要插入名为reservation的表并且正在插入的列是total,那么您将使用以下函数:

public function insert_into_db()
{
   $people =$this->input->post('No.of. People');
   return $this->db->insert('reservation', array('total'=>$people));

}

与评论中提到的F​​red一样,您应该为输入框使用更好的名称。在这两件事之间它应该解决你的问题。