I have the following loop to add several users to a database, however it only works when only one user is added into a form.
echo "The following names have been added to $cname<br><br>";
$query = "";
for ($i = 0; $i < count($fname); $i++) {
$query = $query . "INSERT INTO Students (firstName, lastName, year, class, school, email) "
." VALUES ('{$fname[$i]}','{$lname[$i]}','{$year}','{$cname}','{$sch}','{$uname[$i]}'); ";
echo $fname[$i] . " " . $lname[$i] . " " . $uname[$i];
}
if ($mysqli->query($query)) {
$id = $mysqli->insert_id;
error_log("Inserted {$firstname} as ID {$id}");
return true;
} else {
error_log("Problem inserting {$query}");
return false;
}
Do I just need to add ($mysqli->query($query) to each iteration? Or just move the 'if' clause inside the loop? Or is there a better way.
Thank you.