I am trying to put a 2D array into a database. My code is as follows (this is PHP):
public function addspot($linearray,$data){
$dbname=$data['dbname'];
try {
/* Create a connections with the supplied values */
$pdo = new PDO("mysql:host=" . Config::read('hostname') . ";dbname=" . Config::read('database'). "", Config::read('username'), Config::read('password'), array(PDO::ATTR_PERSISTENT => true));
} catch(PDOException $e) {
/* If any errors echo the out and kill the script */
return 'Database conncetion fail in assets/garage.class.php!Make sure your database information is correct';
}
foreach ($linearray as $lines) {
$spot="INSERT INTO `$dbname`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('$lines[0]', '$lines[1]', '$lines[2]', '$lines[3]', '$lines[4]', CURRENT_TIMESTAMP);";
$statement = $pdo->prepare($spot);
if($statement->execute()){
//silent
} else {
return 'Spot not added!';
}
}
}
The config values are being read correctly, as well as the statement to add a spot is correct. I know this because when I run the function it correctly adds 1 "spot" but not the rest of the rows in my 2D array.
My array is as follows:
array (size=16)
0 =>
array (size=5)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '1' (length=1)
3 => string '0' (length=1)
4 => string '1' (length=1)
1 =>
array (size=5)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '1' (length=1)
3 => string '0' (length=1)
4 => string '1' (length=1)
(and onwards)
My issue is that the function I have written only writes the first line (line[0]) to the database and the other ones do not get written.
Update Output (using print_r) of statements: Placed after the prepare
PDOStatement Object
(
[queryString] => INSERT INTO `Garage2`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('1', '1', '1', '0', '1', CURRENT_TIMESTAMP);
)
PDOStatement Object
(
[queryString] => INSERT INTO `Garage2`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('1', '2', '1', '0', '1', CURRENT_TIMESTAMP);
)
print_r($pdo->errorInfo()); output Placed in the else (fail) part of the execute statement
Array
(
[0] => 00000
[1] =>
[2] =>
)