问题:

使用thinkphp5的insertAll的批量新增函数,提示
SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 2


解答方案:

这个问题是我们在批量插入数组的时候,有几个地方没有考虑到

第一:在第二行数据开始,插入的,每行数据的值的个数和列的字段的个数不相同。也就是假如我们需要插入10个数组,但是每个数组的值不相同,就比如第一个数组里我们有10个,而第二个里面只有5个。

例如:

array(2) {
  [0] => array(8) {
    ["start_time1"] => string(19) "2021-08-01 14:30:00"
    ["end_time1"] => string(19) "2021-08-01 18:30:00"
    ["date"] => string(10) "2021-08-01"
    ["start_time"] => string(19) "2021-08-01 07:40:00"
    ["end_time"] => string(19) "2021-08-01 12:00:00"
    ["admin_id"] => string(3) "044"
    ["is_admin"] => int(2)
    ["warehouse_id"] => int(3)
  }
  [1] => array(6) {
    ["date"] => string(10) "2021-08-01"
    ["start_time"] => string(19) "2021-08-01 15:00:00"
    ["end_time"] => string(19) "2021-08-01 22:00:00"
    ["admin_id"] => string(8) "153     "
    ["is_admin"] => int(0)
    ["warehouse_id"] => int(3)
  }
}

以上就是两个数组的值数量不相同

第二种:数组的键出现了跳跃

例如

array(2) {
  [0] => array(8) {
    ["start_time1"] => string(19) "2021-08-01 14:30:00"
    ["end_time1"] => string(19) "2021-08-01 18:30:00"
    ["date"] => string(10) "2021-08-01"
    ["start_time"] => string(19) "2021-08-01 07:40:00"
    ["end_time"] => string(19) "2021-08-01 12:00:00"
    ["admin_id"] => string(3) "044"
    ["is_admin"] => int(2)
    ["warehouse_id"] => int(3)
  }
  [3] => array(6) {
    ["date"] => string(10) "2021-08-01"
    ["start_time"] => string(19) "2021-08-01 15:00:00"
    ["end_time"] => string(19) "2021-08-01 22:00:00"
    ["admin_id"] => string(8) "153     "
    ["is_admin"] => int(0)
    ["warehouse_id"] => int(3)
  }
}
我们可以看下数组下标一个是0第二个直接变成了3这个也会出现这个报错。只要解决这两个问题数组就可以批量插入到数据库了

点赞(0) 打赏

Comment list 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部