1

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply. Is there any way of using <br> as the separator?
Of course. Just replace ", " to "<br>" in implode call.
I tried that but my email didn't render the HTML. Is this a problem with my client? Or could '\r' work?
Create another question and provide the code you're using to send emails in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.