Reputation: 37
I have some php variables that capture multiple data in the form of an array and i want to input these values in a mysql table.
$surname = unserialize(base64_decode($_POST['surname']));
$firstname = unserialize(base64_decode($_POST['firstname']));
$othername = unserialize(base64_decode($_POST['othername']));
$dob = unserialize(base64_decode($_POST['dob']));
$gender = unserialize(base64_decode($_POST['gender']));
$email = unserialize(base64_decode($_POST['email']));
$phone = unserialize(base64_decode($_POST['phone']));
$location = unserialize(base64_decode($_POST['location']));
$stmt = $connQlife->prepare("INSERT INTO cimbooking (surname, firstname, othername, dob, gender, email, phone, location) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
foreach($surname as $key=>$sname){
$stmt->bind_param('ssssssss', $sname, $firstname[$key], $othername[$key], $dob[$key], $gender[$key], $email[$key], $phone[$key], $location[$key]);
$stmt->execute();
}
$stmt->close();
What happens when this runs is what i can't seem to understand. Looking at the code, this should work just fine. Now in a scenario where the number of records is supposed to be 3, I should see 3 records in the table after submission, but only the first record is being entered into the mysql table. Furthermore, on troubleshooting, i find that when the code runs, the first record that enters the table occupies say id 1. If i refresh the page so the code runs again, the first record of that new command occupies id 4 and not id 2 like it should be. (The id field in the database is already set to auto increment).
Which seems to me like when one record enters the table, the other records enter and disappear or something. I don't seem to understand why.
Upvotes: 0
Views: 33
Reputation: 404
I believe the problem is that once you've called $stmt->bind_param()
then you can't do it again. So the $stmt->execute()
is submitting the same data to the database each time. For some reason this is failing due to, I guess, some duplication rules.
Try using $stmt->bind_result()
to see what happens when you bind and $stmt->get_result()
to get the result of each execute.
I think you need to use $stmt->reset()
after each execution so you can do the next one.
PHP.net manual for mysqli_stmt has links to all the above methods.
Upvotes: 1