Page 1 of 1
How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 3:23 am
by simonmlewis
I'm sure I am missing something silly here.
This is a basic setup where once a success is made, all the entrants in the database are populated into the $body of the email, row by row.
But I cannot seem to see how to place them in the $body tag. Maybe I need a \n somewhere, but I just cannot suss it out. Help please.
Code: Select all
if ($success == "yes")
{
mysql_query("UPDATE register SET paid = 'yes' WHERE sessionid = '$sessionid'");
$to = "email@site.com";
$subject = "Booking";
$headers = "From: info@site.com";
$body = " .
$result = mysql_query ("SELECT * FROM register WHERE sessionid = '$sessionid' AND paid = 'yes'");
while ($row = mysql_fetch_object($result))
{
if ($row->leaderattending == "yes") { echo "Leader attending: $row->leaderattending";
echo "
$row->title $row->firstname $row->lastname
$row->telephone
$row->mobiletelephone
$row->email
Company (if applicable): $row->company
$row->address1
$row->address2
$row->town
$row->postcode
$row->country
Special Needs (if applicable: $row->specialneeds
Where did they hear about us: $row->wheredidyouhear
$row->datebooked
$row->dateofbooking
Paid: $row->paid
-----------------
";
}
mysql_free_result($result);
. ";
mail ($to, $subject, $body, $headers);
}
echo "<script>
window.location.replace('/register_c&success=yes')
</script>";
unset($_SESSION['sessionid']);
}
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 4:37 am
by Christopher
Shouldn't it be "$body .=" instead of "echo" ?
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 4:43 am
by simonmlewis
Like this?
Or do I somehow append each echo " result in there??
Code: Select all
if ($success == "yes")
{
mysql_query("UPDATE register SET paid = 'yes' WHERE sessionid = '$sessionid'");
$to = "email@site.com";
$subject = "Booking";
$headers = "From: info@site.com";
$body .=
$result = mysql_query ("SELECT * FROM register WHERE sessionid = '$sessionid' AND paid = 'yes'");
while ($row = mysql_fetch_object($result))
{
if ($row->leaderattending == "yes") { echo "Leader attending: $row->leaderattending";
echo "
$row->title $row->firstname $row->lastname
$row->telephone
$row->mobiletelephone
$row->email
Company (if applicable): $row->company
$row->address1
$row->address2
$row->town
$row->postcode
$row->country
Special Needs (if applicable: $row->specialneeds
Where did they hear about us: $row->wheredidyouhear
$row->datebooked
$row->dateofbooking
Paid: $row->paid
-----------------
";
}
mysql_free_result($result);
. ";
mail ($to, $subject, $body, $headers);
}
echo "<script>
window.location.replace('/register_c&success=yes')
</script>";
unset($_SESSION['sessionid']);
}
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 6:05 am
by Celauran
The line before mail() looks like it needs to be removed. That otherwise should work. Does it?
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 6:49 am
by simonmlewis
Sorry, what if I need to add some text in the bottom of the eamil, like "System run by Simon"?
How do I append that into the bottom of the code, below mail()?
. "System designed by Simon"; ???
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 7:24 am
by Celauran
simonmlewis wrote:Sorry, what if I need to add some text in the bottom of the eamil, like "System run by Simon"?
How do I append that into the bottom of the code, below mail()?
. "System designed by Simon"; ???
You don't. If you want it to be included in the mail, it has to come before sending the mail.
I had missed that you're still using 'echo'. Serves me right for replying before having had coffee. You need to get rid of the echo and append to your $body variable as you iterate over the results.
Code: Select all
$body = '';
foreach ($results as $result) {
$body .= "whatever";
}
$body .= "System by Simon";
mail($to, $subject, $body, $headers);
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 7:28 am
by simonmlewis
Sorry you have lost me now.
What's that about foreach - where is my $row-> code??
I know what you mean about the coffee. As a side, since ur in Canada, what is your preferred coffee? Having been to the States before, I like Folgers and Yuban. Some some empty tins in my kitchen from my adventures there.
Back to the job though, where in that code does my database query go?
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 7:33 am
by Celauran
Was just dummy code to avoid having to copy/paste 50 lines of code.
Code: Select all
if ($success == "yes") {
mysql_query("UPDATE register SET paid = 'yes' WHERE sessionid = '$sessionid'");
$to = "email@site.com";
$subject = "Booking";
$headers = "From: info@site.com";
$result = mysql_query ("SELECT * FROM register WHERE sessionid = '$sessionid' AND paid = 'yes'");
while ($row = mysql_fetch_object($result)) {
$body = '';
if ($row->leaderattending == "yes") {
$body .= "Leader attending: $row->leaderattending";
$body .= "
$row->title $row->firstname $row->lastname
$row->telephone
$row->mobiletelephone
$row->email
Company (if applicable): $row->company
$row->address1
$row->address2
$row->town
$row->postcode
$row->country
Special Needs (if applicable: $row->specialneeds
Where did they hear about us: $row->wheredidyouhear
$row->datebooked
$row->dateofbooking
Paid: $row->paid
-----------------
";
}
mysql_free_result($result);
mail ($to, $subject, $body, $headers);
}
echo "<script>
window.location.replace('/register_c&success=yes')
</script>";
unset($_SESSION['sessionid']);
}
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 7:38 am
by Celauran
Note that could be slightly off as you had mismatched braces, so check the nesting carefully. It's currently sending one email per result, which is what the code suggested but not the title of the thread.
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 7:46 am
by simonmlewis
This is throwing an error on line 44:
[text]Notice: Undefined variable: body in C:\xampp\phpMyAdmin\site\includes\register_complete.inc on line 44[/text]
Line 44 is:
Code: Select all
$body .= "--------------------------------------------
System designed and maintained by me";
Code: Select all
if ($success == "yes")
{
mysql_query("UPDATE register SET paid = 'yes' WHERE sessionid = '$sessionid'");
$to = "judith@site.com";
$subject = "Booking";
$headers = "From: info@site.com";
$result = mysql_query ("SELECT * FROM register WHERE sessionid = '$sessionid' AND paid = 'yes'");
while ($row = mysql_fetch_object($result))
{
$body = "";
if ($row->leaderattending == "yes")
{
$body .= "Leader attending: $row->leaderattending";
}
$body .= "
$row->title $row->firstname $row->lastname
$row->telephone
$row->mobiletelephone
$row->email
Company (if applicable): $row->company
$row->address1
$row->address2
$row->town
$row->postcode
$row->country
Special Needs (if applicable: $row->specialneeds
Where did they hear about us: $row->wheredidyouhear
$row->datebooked
$row->dateofbooking
Paid: $row->paid
-----------------";
}
mysql_free_result($result);
$body .= "--------------------------------------------
System designed and maintained by me";
mail ($to, $subject, $body, $headers);
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 7:51 am
by Celauran
$body is being defined inside the while loop. If you have an empty result set, it isn't defined. Move that outside the loop.
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 7:54 am
by simonmlewis
Got it. Ta.
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 9:18 am
by simonmlewis
At the end of the code, it should be clearing the sessionid, but it's not working.
It runs through the code, unsets it, and then moves on.
But when I test the form again, the same session ID is being used.
Code: Select all
$result = mysql_query ("SELECT * FROM register WHERE sessionid = '$sessionid' AND paid = 'yes'");
while ($row = mysql_fetch_object($result))
{
if ($row->attending == "Yes")
{
$attendingtext = "Information only for person registering person or persons. Your invoice will be sent via PayPal, along with number of attendees and fees paid.";
}
else
{
$attendingtext = "";
}
$to = "$row->email";
$subject = "site Booking";
$headers = "From: judith@site.com";
$body = "$attendingtext
Your Booking with site details shown below:
Booking ID: $row->sessionid
$row->title $row->firstname $row->lastname
Special Needs (if applicable): $row->specialneeds
$row->datebooked
$row->dateofbooking
-----------------
Kind regards
site.com";
mail ($to, $subject, $body, $headers);
}
mysql_free_result($result);
unset($_SESSION['sessionid']);
echo "<script>
window.location.replace('/register_c&success=yes')
</script>";
Right at the start of the process, I use this for the Session ID:
Code: Select all
if (isset($_SESSION["sessionid"]))
{
$sessionid = $_SESSION["sessionid"];
}
else
{
$sessionid = session_id();
$_SESSION['sessionid'] = $sessionid;
}
Re: How do I echo multiple DB rows in one email?
Posted: Thu Feb 26, 2015 2:16 pm
by simonmlewis
I thought you could do it via the *.inc file at the bottom. I know the session() start script has to be in the *.php, but not the clear session. What am I doing wrong??