php - MySQL插入查询一次插入多条记录

标签 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 条记录:一条记录​​包含所有正确值,其余 4 条记录包含空值。

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

插入表格后看起来像这样:

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   

到底出了什么问题?

最佳答案

试试这个:

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

关于php - MySQL插入查询一次插入多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14119182/

相关文章:

php - 如何获取其他表使用codeigniter的对象的行数

php - 如何使用php webservice查找安装在用户手机中的应用程序版本

php - codeigniter,如何获取多个查询并从模型中对其进行计数

php - 我怎样才能从这个数组中提取值?

php - 在登录时更改 Facebook 重定向 URL

mysql - 如何评价数据库系统中一张表的好坏?

最后一个 CSV 列中的 MySQL NULL 可能

php - PHP Pthread 类中的 Yii 框架上下文

php - 使用 PHP for 和 foreach 循环从 JSON 数据中检索视频信息时遇到问题 - YouTube API 3

c# - Entity Framework 6.1 Code First MySql 实体跟踪在上下文中持续存在