I have a while statement that’s executing correctly but it needs to be a foreach statement since there are multiple inserts being made and it’s not generating the random password or sending the email for each. Here’s the code:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$password = createRandomPassword();
// Add the user to the database:
$sql = "INSERT into users(account_id,first,last,email,pass) values('$id', '$data[0]','$data[1]','$data[2]','$password')";
$r = mysqli_query ($dbc, $sql) or trigger_error("Query: $sql\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
$emailBody = "<html><body>";
$emailBody .= "<br><br><b>Hey $first $last!</b>";
$emailBody .= "<br><br>You've been added to our site and your password is: $password";
$emailBody .= "</body></html>";
mail("$ln_first $ln_last <$ln_email>", "Your Account" , $emailBody, "From: YourCompany <noreply@yourcompany.com>\nContent-Type: text/html");
}
fclose($handle);
echo '<p><div class="alert green">You have successfully imported users.</div></p>';
}
I don’t think there’s anything wrong with your
whileloop – you’re closing the handle too early! Thefcloseand finalecholines should be after that final curly bracket.