MySQL插入查询一次插入多个记录

时间:2013-01-02 08:55:22

标签: php mysql codeigniter phpmyadmin

我在项目中有以下代码:

$start_time = $this->input->post("start_time");
$a          = explode(":",$start_time);
$start_sec  = ($a[0]*3600)+($a[1]*60);
$end_sec    = $start_sec+5400;
$m          = ($end_sec/60);
$hrs        = (int)($m/60);
$mins       = ($m%60);
$sec        = "00";
$end_time   = $hrs.":".$mins.":".$sec;
$start_time .= ":".$sec;


$tablearr=array(
  "rest_id"        => $info['rest_id'],
  "rest_sec_id"    => $info['rest_sec_id'],
  "book_date"      => $this->input->post("date"),
  "book_start_time"=> $this->input->post("start_time"),
  "book_end_time"  => $end_time,
  "book_special"   => $this->input->post("purpose"),
  "book_cuisine"   => $this->input->post("cuisine"),
  "book_no_person" => $this->input->post("no_person"),
  "user_id"        => $this->session->userdata('member_id'),
  "book_is_menu"   => $this->input->post("menu"),
  "table_reserved" => $info['table_reserved']
);

$this->db->insert("b_rest_book",$tablearr);

这应该插入“预订”的单个记录。但是在查询中插入了5条执行记录:一条记录​​包含所有正确值,另外四条记录保留空值。

用户输入开始时间,结束时间根据开始时间计算。

插入表后看起来像这样:

book_id     rest_id     rest_sec_id     book_date   book_start_time     book_end_time   book_special    book_cuisine    book_no_person  user_id     book_is_menu    table_reserved  special_request

1   1   2   2012-12-23  07:00:00    08:30:00    2   2   2   2   No  1 

2   1   0   0000-00-00  00:00:00    01:30:00    0   0   0   2       1 

3   1   0   0000-00-00  00:00:00    01:30:00    0   0   0   2       1 

4   1   0   0000-00-00  00:00:00    01:30:00    0   0   0   2       1    
5   1   0   0000-00-00  00:00:00    01:30:00    0   0   0   

究竟出了什么问题?

1 个答案:

答案 0 :(得分:2)

试试这个:

if($info['rest_sec_id'] != 0)
{
   $this->db->insert("b_rest_book",$tablearr);
}