I have an array of values that I am trying to insert into a database. Currently the below only inserts the first key value pair in the array and then returns an error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in /_/components/php/functions.php:33
Stack trace:
0 /_/components/php/functions.php(33): PDOStatement->fetchAll()
1 /testdb.php(44): Photo\DB\query('INSERT into cat...', Array, Object(PDO))
2 {main} thrown in /_/components/php/functions.php on line 33
line 33 in functions.php is in this function
function query($query, $bindings, $conn){
$stmt = $conn->prepare($query);
$stmt->execute($bindings);
$results = $stmt->fetchAll(); <---line 33
return $results ? $results : false;
}
The main code is
foreach ($sitecategories as $key => $value) {
// print "this is key $key and this is value $value";
if ( $conn ) {
$catquery=query("INSERT into categories (cat_id, label) VALUES (:catid, :label)",
array('catid'=>$key, 'label'=>$value),
$conn);
} else {
print "could not connect to the database";
}
}
So I am connecting OK to the DB (elsewhere in the code) and the first key value pair is inserted successfully but not the rest of the array. I'm guessing my syntax is incorrect somewhere in
array('catid'=>$key, 'label'=>$value),
but I can't figure it out. Any help much appreciated!