Last active
August 29, 2015 14:27
-
-
Save terrymun/32ffc0a49982782397e7 to your computer and use it in GitHub Desktop.
Executing multiple prepared statements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming that database connection is already open | |
// Receive user input that is separated by a new line for each row | |
$favouriteFoods = array('Apple', 'Poutine', 'Cheesestrings', 'Pickle chips', 'Peanut butter and raisins'); | |
$user = 'johndoe'; | |
// Prepare statement | |
$stmt = $db->prepare("INSERT INTO users (user, foodItem) VALUES(:username, :favouriteFood)"); | |
$db->beginTransaction(); | |
// Insert a new row for each food item | |
foreach($favouriteFoods as $food) { | |
$db->execute(array(':username'=>$user, ':favouriteFood'=>$food)); | |
} | |
$db->commit(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment