I'm trying to dynamically create an array for the execute portion of a PDO insert. My working code includes text like this:
$stmt->execute(array(
':fname' => $_POST['fname'],
':lname' => $_POST['lname'],
':email' => $_POST['email']
));
My first attempt at creating an array:
$insert_execute = array();
foreach ($values as $column => $value) {
array_push($insert_execute, ":" . $column . " => " . "'".$value."'");
}
But when I try to execute I get a PDO error:
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
And when I print the result, there is an additional parameter:
Array
(
[0] => fname => 'Test Name'
[1] => lname => 'Test Last'
[2] => email => '[email protected]'
)
How can I modify the array_push to remove the [0], [1], [2] etc. in the array?