Im slowly getting the hang of PDO and prepared statements, but could someone please clarify the following:
I understand you can insert data from an array using the following code:
$values = array('bill', 'ben', 'bob');
$stmt = $db->prepare("INSERT INTO first_page_data(first_name) VALUES(:fname)");
$stmt->bindParam(':fname', $first_name, PDO::PARAM_STR);
foreach ($values as $first_name) {
$stmt->execute();
}
This will insert the three names into 3 new rows in the database, no problems here!
However if I have an array containing the database column names and a corresponding array containing the values, is there a way of inserting multiple columns for the same user in one prepared statement rather than typing out the (rather monotonous) line of code:
$stmt = $db->prepare("INSERT INTO first_page_data(first_name,surname,phone_no,email,postcode) VALUES(:fname,:sname,:phone,:email,:postcode)");
IE in the brackets after the table name, just have an array variable rather than listing out the column names.
Also if I use unnamed placeholders in my prepared statement, can I iterate through the POST array from a form to bind the values to the ?
placeholders? (instead of typing multiple $stmt->bindValue(.......)
lines?