使用单个查询PHP(Prepared语句)插入多行

时间:2016-06-15 02:17:26

标签: php mysql

使用PHP预处理语句在单个查询中插入多行的最佳方法是什么?

我有这个多维数据:

$data = array(
'6' => array(
  '945' => array(
    '11:04' => array(
      '0' => array(
        'date' => '2013-02-02',
        'user' => '3'
      ),
    ),
    '11:05' => array(
      '0' => array(
        'date' => '2013-02-03',
        'user' => '3'
       ),
    ),
    '11:06' => array(
      '0' => array(
        'date' => '2013-02-03',
        'user' => '3'
       ),
    )
  )
);

我使用的查询是:

$ins = $this->conn->prepare("INSERT INTO table (date, user) VALUES ('2013-02-03', '03' )"); $ins->execute();

上面的查询正在运行,但它只插入一行,所以我尝试使用foreach如下:

 foreach ($data as $dk => $dv) {
    foreach ($dv as $k => $v) {
        foreach ($v as $rk => $rv) {
           $ins = $this->conn->prepare("INSERT INTO table  (date, user) VALUES ($rv['date'], $rv['user'] )");
            $ins->execute();

        }
    }
}

我想查询为:

INSERT INTO emp (date,user) VALUES("2013-02-02",3),("2013-02-02",3),("2013-02-03",3);

请帮忙

0 个答案:

没有答案