Form to Email code problems (while - looping)

I have a form on a page which is accessed when a user logs into their account.
The form dynamically pulls bits of the users info from 2 tables in a mysql database, Tables b[/b] and Table b[/b].
Table b[/b] holds the users info (NAME, EMAIL ETC) while Table b[/b] hold information about the users items for which they may have several of.
When the form page is accessed all the users items from Table (ITEMS) are dynamically shown on the form with several radio buttons at the side of each item.
The user can tick certain radio buttonss at the side of each item and click the send button.

TRYING TO ACHIEVE…
After the form is sent a thank you message is returned on the same page with details of the items and what radios were ticked.
An Email of the same is sent to user with info of the items and what radios were ticked.
An Email is also sent to me with same info of the items and what radios were ticked.

WHAT I AM ACHIEVING SO FAR WITH CODE BELOW:

The form page is ok showing all users items, radios, send button etc.
The returned thank you page is fine showing items, what radios were checked etc.
Even the email part is working sending an email to the user and myself.

THE PROBLEM IS THAT A SEPERATE EMAIL IS SENT TO BOTH OF US FOR EACH ITEM THE USER HAS IN THE DATABASE.
I JUST WANT ONE EMAIL SENDING TO BOTH OF US DETAILING ALL ITEMS.

I realise this is because of the while statement in the call to the database which is looping the below code for each item thus sending an email for each item.

What I can not suss out is how to send just the one email with all items on.
Tried moving the closing loop bracket all over the place and parts of the code but to not avail.

THE CODE: (some unecessary code removed from echo/thank you to simplify)

"> <?php $sql = "SELECT * FROM ".$TABLES["items"]." WHERE user_id='".$_SESSION["UserAccount"]."'"; $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); while ($ITEMS = mysql_fetch_assoc($sql_result)) { $sql = "SELECT * FROM ".$TABLES["users"]." WHERE id='".$ITEMS["user_id"]."'"; $sql_resultT = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); $USER=mysql_fetch_assoc($sql_resultT); ?> <? if (isset($action)) { $to = $USER["email"]; $mailheader = "From: $email\r\n"; $mailheader .= "Reply-To: $email\r\n"; $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; $msg = "Dear ".stripslashes($USER["name"])."

Name Ref Status Type Date Extend Feature Offer
Name Ref No Status Type Date Extend Feature Offer
".stripslashes($ITEMS["name"])." ".stripslashes($ITEMS["ref"])." ".stripslashes($ITEMS["status"])." ".stripslashes($ITEMS["type"])." ".stripslashes($ITEMS["date"])." ".$_POST['extend']." ".$_POST['feature']." ".$_POST['offer']."
"; mail($to, "subject", $msg, $mailheader) or die ("Failure"); mail("[email protected]", "subject", $msg, $mailheader) or die ("Failure"); echo "

Thank you!
Your details of your request has been sent:
"; }; ?> <?php echo stripslashes($ITEMS["name"]); ?> <?php echo stripslashes($ITEMS["ref"]); ?> <?php echo stripslashes($ITEMS["status"]); ?> <?php echo stripslashes($ITEMS["type"]); ?> <?php echo stripslashes($ITEMS["date"]); ?> " value="No"> No " value="Yes" /> Yes " value="No"> No " value="Yes" /> Yes " value="No"> No " value="Yes" /> Yes <?php } ?>  

Here is how you need to update your code: move headers of email outside of the while loop, same with mail() commands - they need to be after loop. Part of email body need to be generated before loop, while items added within the loop:

[php]

"> <?php $sql = "SELECT * FROM ".$TABLES["items"]." WHERE user_id='".$_SESSION["UserAccount"]."'"; $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); if (isset($action)) { $to = $USER["email"]; $mailheader = "From: $email\r\n"; $mailheader .= "Reply-To: $email\r\n"; $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; $msg = "Dear ".stripslashes($USER["name"])."

Name Ref Status Type Date Extend Feature Offer
"; } while ($ITEMS = mysql_fetch_assoc($sql_result)) { $sql = "SELECT * FROM ".$TABLES["users"]." WHERE id='".$ITEMS["user_id"]."'"; $sql_resultT = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); $USER=mysql_fetch_assoc($sql_resultT); $msg.=" "; ?> <?php } // while() loop end ?>
Name Ref No Status Type Date Extend Feature Offer
".stripslashes($ITEMS["name"])." ".stripslashes($ITEMS["ref"])." ".stripslashes($ITEMS["status"])." ".stripslashes($ITEMS["type"])." ".stripslashes($ITEMS["date"])." ".$_POST['extend']." ".$_POST['feature']." ".$_POST['offer']."
<?php echo stripslashes($ITEMS["name"]); ?> <?php echo stripslashes($ITEMS["ref"]); ?> <?php echo stripslashes($ITEMS["status"]); ?> <?php echo stripslashes($ITEMS["type"]); ?> <?php echo stripslashes($ITEMS["date"]); ?> " value="No"> No " value="Yes" /> Yes " value="No"> No " value="Yes" /> Yes " value="No"> No " value="Yes" /> Yes
 
<?php if (isset($action)) { $msg.=""; mail($to, "subject", $msg, $mailheader) or die ("Failure"); mail("[email protected]", "subject", $msg, $mailheader) or die ("Failure"); echo "

Thank you!
Your details of your request has been sent:
"; } ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service