简体   繁体   中英

Foreach loop within $message of PHP mail() function

I have a loop that enters each non-empty text field into a database:

foreach ($guests as $guest) {

    if ($guest !== "") {
        mysql_query("INSERT INTO guestlists (guest, night, date) VALUES('$guest', '$night', '$date') ") or die(mysql_error());
    }
}

I'd like to also print each of these names into an email, which I presume somehow containing the output within the $message variable in mail(). How is this best achieved?

On a side note, changing from by editing $headers is proving very temperamental. What is the proper way of doing this?

For example:

$names = array();
foreach ($guests as $guest) {
    if ($guest !== "") {
        mysql_query("INSERT INTO guestlists (guest, night, date) VALUES('$guest', '$night', '$date')") or die(mysql_error());
        $names[] = $guest;
    }
}
$message = "Guests: ".implode(", ", $names).". ";

I hope you're sure that $guest, $night, $date variables are already passed through mysql_real_escape_string function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM