I have a combined array of the type [key] => value and want to insert it into mysql table using PDO prepared statement. My code is as shown below:
$a = array('avocado', 'apple', 'banana');
$b = array('green', 'red', 'yellow');
$c = array_combine($a, $b);
print_r($c);
$stmt = $pdo->prepare("INSERT INTO fruits (name, color) VALUES (?,?)");
try {
$pdo->beginTransaction();
foreach ($c as $row)
{
$stmt->execute($row);
}
$pdo->commit();
}catch (Exception $e){
$pdo->rollback();
throw $e;
}
However, when I execute the statement, I get an error code:
Warning: PDOStatement::execute() expects parameter 1 to be array, string given in C:\xampp\htdocs\mysites\PDOmysql_tutorial\pdoInsert.php on line 19. Could I get a little help to correct my code or a comment if I am using the right approach. Thanks