简体   繁体   English

查询只插入一行而不是多行

[英]query insert only one row instead multiple rows

I was trying to insert multiple values from the excel sheet, I got all data from excel sheet to an array variable. 我试图从excel工作表插入多个值,我将所有数据从excel工作表存储到数组变量中。 but when I insert it always inserts the 1st row only, I have a method to execute insert query by using PDO, 但是当我插入它总是只插入第一行时,我有一种方法可以使用PDO执行插入查询,

My Data 我的资料

array (size=4)
  1 => 
    array (size=5)
      'A' => string '*' (length=1)
      'B' => string 'Title' (length=5)
      'C' => string 'Author' (length=6)
      'D' => string 'Publication ' (length=12)
      'E' => string 'Container' (length=9)
  2 => 
    array (size=5)
      'A' => float 1
      'B' => string 'Test' (length=4)
      'C' => string 'one' (length=3)
      'D' => string 'two' (length=3)
      'E' => string 'X1' (length=2)
  3 => 
    array (size=5)
      'A' => float 2
      'B' => string 'Test' (length=4)
      'C' => string 'three' (length=5)
      'D' => string 'four' (length=4)
      'E' => string 'X2' (length=2)
  4 => 
    array (size=5)
      'A' => float 3
      'B' => string 'Test' (length=4)
      'C' => string 'five' (length=4)
      'D' => string 'six' (length=3)
      'E' => string 'X3' (length=2)

This my method 这是我的方法

public function importBooks($data, $nr)
{
    // Init query
    $this->db->query('INSERT INTO books_pre (title, author, publication, container, created_by, created_at) VALUES (:title, :author, :publication, :container, :created_by, now())');

    for ($i=2; $i<$nr; $i++) {
        // Bind values
        $this->db->bind(':title', $data[$i]['B']);
        $this->db->bind(':author', $data[$i]['C']);
        $this->db->bind(':publication', $data[$i]['D']);
        $this->db->bind(':container', $data[$i]['E']);
        $this->db->bind(':created_by', $_SESSION['user_id']);

        // Execute query
        if ($this->db->execute()) {
            return true;
        } else {
            return false;
        }
    }
}

The return statement will end the execution of your method -- and exit the loop. return语句将结束方法的执行-并退出循环。 To quote the manual: 引用手册:

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. 如果从函数内部调用,则return语句将立即结束当前函数的执行,并将其参数作为函数调用的值返回。

To make this work, with minimal effort, I'd only return false when $this->db->execute() fails and return true at the end of your method, like so: 为了使此工作轻松进行,我只会在$this->db->execute()失败时return false ,并在方法末尾return true ,如下所示:

public function importBooks($data, $nr)
{
    // Init query
    $this->db->query('INSERT INTO books_pre (title, author, publication, container, created_by, created_at) VALUES (:title, :author, :publication, :container, :created_by, now())');

    for ($i=2; $i<$nr; $i++) {
        // Bind values
        $this->db->bind(':title', $data[$i]['B']);
        $this->db->bind(':author', $data[$i]['C']);
        $this->db->bind(':publication', $data[$i]['D']);
        $this->db->bind(':container', $data[$i]['E']);
        $this->db->bind(':created_by', $_SESSION['user_id']);

        // Execute query
        if (!$this->db->execute()) {
            return false;
        }
    }

    return true;
}

However, if I had to rewrite this code, I'd personally probably implode the data array and insert it all in one query. 但是,如果必须重写此代码,我个人可能会爆破数据数组并将其全部插入一个查询中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM