I was trying to insert multiple values from the excel sheet, I got all data from excel sheet to an array variable. but when I insert it always inserts the 1st row only, I have a method to execute insert query by using 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;
}
}
}