I am trying to insert multiple records in a for loop like so:
$connection = mysqli_connect("localhost", "username", "password");
mysqli_select_db($connection, "database");
for ($i = 1; $i <= $_POST['people']; $i++) {
$stmt = "";
if ($stmt = $connection->prepare("INSERT INTO `table` (firstname, lastname, email, rsvp) VALUES (?,?,?,?)")) {
$stmt->bind_param('ssss', "James", "Smith", "smith@abc.com", "yes");
$stmt->execute();
$stmt->close();
}
}
mysql_close($connection);
But its not inserting, I put in an echo at the beginning of the loop and it only echos once. Please help.
Try assigning all of your customer data to variables first, like so:
$firstname = 'James';
$lastname = 'Smith';
$email = 'smith@abc.com';
$rsvp = 'yes';
$stmt->bind_param('ssss', $firstname, $lastname, $email, $rsvp);