So I have a dynamic table on the page 2 where the user can add as many entries as they want. After submitting the 3 page is getting all the info just fine. The problem is my for-loop iteration through the array is not working. Here’s the code:
for($i = 0; $i < sizeof($_POST["fname_new"]); $i++) {
$fname_new = $_POST["fname_new"][i];
$lname_new = $_POST["lname_new"][i];
$phone_new = $_POST["phone_new"][i];
$email_new = $_POST["email_new"][i];
$ethnicity_new = $_POST["ethnicity_new"][i];
$stmt = $link -> prepare("INSERT INTO Conference (`First Name`, `Last Name`, `Phone`, `Email`, `Ethnicity`) VALUES (:first_new, :last_new, :phone_new, :email_new, :ethnicity_new)");
$stmt->bindParam(':first_new', $fname_new);
$stmt->bindParam(':last_new', $lname_new);
$stmt->bindParam(':phone_new', $phone_new);
$stmt->bindParam(':email_new', $email_new);
$stmt->bindParam(':ethnicity_new', $ethnicity_new);
$stmt->execute();
}
So when I do
echo sizeof($_POST["fname_new"]);
It shows me the correct number, so if there were 3 rows that the user added on page 2 then the value echoed should be 3, which it is. When I do:
echo $_POST["fname_new"][0];
Then it appropriately gives the first name from the first row. I can replace 0 with any number, as long as it is in bounds, and it is correct. But for some reason when I do
echo $_POST["fname_new"][i];
It doesn’t print anything. I have no idea why this for loop isn’t working. Logically it is correct. Any ideas?
You forgot the $ to point to the variable i
Try this: