I want to store data from an array into my database.
This is how my Array looks when I do print_r($animals);:
Array
(
[0] => Array
(
[animal] => Cat
[name] => Tom
)
[1] => Array
(
[animal] => Dog
[name] => Bob
)
[2] => Array
(
[animal] => Bird
[name] => Sam
)
[3] => ....
This is how I try to store the data, but for some reason it doesn't work, nothing is stored:
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO animals (animal,name) values(?,?) ";
$q = $pdo->prepare($sql);
foreach($animals as $row) {
$q->execute(array(
$row['animal'],
$row['name'],));
}
Database::disconnect();
If I write the following for example the storing works, but only the first entry is stored (Cat and Tom)
foreach($animals as $row) {
$name = $row['name'];
$animal = $row['animal'];
}
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO animals (animal,name) values(?,?) ";
$q = $pdo->prepare($sql);
$q->execute(array($animal,$name));
Database::disconnect();
print_r($animals);what is in$properties?$row['name'],));$propertieswas wrong, it should be$animals, I updated the mistake