kmadden84 Posted July 19, 2012 Share Posted July 19, 2012 Hello everyone. I almost have this down, and I think the problem is how I'm using the while loop. The below script is meant to query my database and pull serial numbers based on email address. The serial numbers found are then sent to the email address. Using the script below - I get multiple emails. E.g. if there's 100 serial numbers, I will get 100 emails. The first email has 1 serial, the second has 2, the third 3, etc etc until it reaches 100. The "Email being sent" message is also repeated 100 times. Am I not closing the while loop? <?php $db = mysql_connect("localhost","user","pass"); if(!$db) die("Error connecting to MySQL database."); mysql_select_db("database" ,$db); $to = $_POST['formEmail']; $varEmail = $_POST['formEmail']; $varName = $_POST['formName']; $email = "information@whatever"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $data = mysql_query("SELECT Serial FROM SerialRecord WHERE Email = '$varEmail'"); if(mysql_num_rows($data) > 0) while($info = mysql_fetch_array( $data )) { $subject = "Serial Number"; $message.="<table border='1'>"; $message.="<tr>"; $message.= "<td>" . $info['Serial'] . "</td>"; $message.="</tr>"; $message.="</table>"; mysql_close($db);{} mail($to,$subject,$message,$headers); ?> <p class="auto-style12">Thank you for your inquiry <i><b><?php echo "$varName"; ?></b></i>. Your email has been sent to <?php echo "$varEmail"; ?> You should receive an email within the next few minutes containing your serial number</p> <?php } else { ?> <p class="auto-style12">Sorry, the email address <i><b><?php echo "$varEmail"; ?></b></i> was not found. Please contact <a href="mailto:support@whatever>Support</a> for further assistance. <br></fp> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265967-while-loop-is-sending-multiple-emails/ Share on other sites More sharing options...
xyph Posted July 19, 2012 Share Posted July 19, 2012 No, you're not closing it where you want to. Quote Link to comment https://forums.phpfreaks.com/topic/265967-while-loop-is-sending-multiple-emails/#findComment-1362910 Share on other sites More sharing options...
kmadden84 Posted July 19, 2012 Author Share Posted July 19, 2012 Thanks for the helpful advice... Can you tell me where and how to close? I've tried using endwhile; after numerous sections but nothing's working. Could you perhaps elaborate a little more? I'd appreciate it very much. Quote Link to comment https://forums.phpfreaks.com/topic/265967-while-loop-is-sending-multiple-emails/#findComment-1362917 Share on other sites More sharing options...
kmadden84 Posted July 20, 2012 Author Share Posted July 20, 2012 Figured it out myself. Working code for anyone interested. <?php $db = mysql_connect("localhost","user","pass"); if(!$db) die("Error connecting to MySQL database."); mysql_select_db("database" ,$db); if($_POST['formSubmit'] == "Submit") $varEmail = $_POST['formEmail']; $varName = $_POST['formName']; $to = $_POST['formEmail']; $email = "information@email.com"; $subject = "Serial Number"; $body.= "Hello $varName. Your Serial Numbers are:<BR/><BR/>"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $data = mysql_query("SELECT Serial FROM SerialRecord WHERE Email = '$varEmail'"); if(mysql_num_rows($data) > 0){ ?> <font size="2" face="Arial, Helvetica, sans-serif">Email found sending now.<br></font> <?php while($info = mysql_fetch_array( $data )) $body.= "<td>" . $info['Serial'] . "</td><BR />"; $body.="</tr>"; $body.="</table>"; mail($to,$subject,$body,$headers); } else { ?> <font size="2" face="Arial, Helvetica, sans-serif">Sorry, the Email Address<i><b><?php echo "$varSerial"; ?></b></i> was not found. Please contact <a href="mailto:support@email.com">Support</a> for further assistance. <br></font> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265967-while-loop-is-sending-multiple-emails/#findComment-1362930 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.