cdoggg94 Posted September 18, 2012 Share Posted September 18, 2012 I have a page that I want to both: email information, and, insert information. Both of the scripts I have work for what I want them to do. My problem is that when the email script is ABOVE the insert script, the email works and the insert doesn't...and the opposite is the same. Here is my code where the email works and the insert doesn't: <?php //-------------email section----------------// error_reporting(-1); $grandTotal = $_GET['Total']; $thisClient = $_GET['FinOrder']; $to = "email@gmail.com"; $subject = "New Order"; $message = " <html> <head> <title>New Order</title> </head> <body bgcolor='#000000' text='#FFFFFF'> <div align='center'> <img src='http://WEBSITE.com/cTrends/images/header.jpg' /> <br /> Client Name: ".$thisClient."<br /><br /> <table border='0' width='650'> <tr> <th width='100' align='center'>LCBO #</th> <th width='370' align='left'>Product</th> <th width='60' align='center'>Unit Price</th> <th width='60' align='center'>Qty</th> <th width='60' align='right'>Total</th> </tr>"; while($row = mysql_fetch_assoc($AllWine)){ $unit_price = "$".number_format(($row['wish_bottle'])/($row['wish_case']), 2); //$unit_price2 = ($unit_price)*($row['wish_case']); $message .= "<tr> <td align='center'>".$row['wish_bottle_LTO']."</td> <td align='left'>".$row['wish_client']."</td> <td align='center'>".$unit_price."</td> <td align='center'>".$row['wish_case']."</td> <td align='right'>$".number_format($row['wish_total'], 2)."</td> </tr>"; } $message .= "<tr> <td colspan='5' align='right'><b>Grand Total = $".number_format($grandTotal, 2)."</b></td> </tr></div></body></html>"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; // More headers $headers .= 'From: <email@gmail.com>'. "\r\n"; mail($to,$subject,$message,$headers); //-------------recording--------------------// $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); while($row = mysql_fetch_assoc($AllWine)){ mysql_query("INSERT INTO recording_tbl (record_id, record_client, record_date, record_info, record_price) VALUES ('', '".$_GET['FinOrder']."', '".$today."', '".$row['wish_client']."<br />', '".$_GET['Total']."')"); } mysql_close($con); ?> Any ideas why they both wouldn't work ? Sorry if its really obvious... Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted September 18, 2012 Share Posted September 18, 2012 I don't see where you've declared $AllWine Quote Link to comment Share on other sites More sharing options...
cdoggg94 Posted September 18, 2012 Author Share Posted September 18, 2012 I didn't add it, but its would be above that code in the previous post like this: <?php mysql_select_db($database_myConnect, $myConnect); $query_AllWine = "SELECT* FROM wish_list WHERE wish_product = '".$_GET['FinOrder']."' ORDER BY wish_client ASC"; $AllWine = mysql_query($query_AllWine, $myConnect) or die(mysql_error()); $totalRows_AllWine = mysql_num_rows($AllWine); ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 You try to loop through it twice. By the second time, the internal pointer is already at the end. You should only loop once, and build your email AND your multi-insert in one block. You should never run queries in loops. You can make the insert in a loop one larger insert. Quote Link to comment Share on other sites More sharing options...
cdoggg94 Posted September 18, 2012 Author Share Posted September 18, 2012 Okay.. So I did this: <?php //-------------email section----------------// error_reporting(-1); $grandTotal = $_GET['Total']; $thisClient = $_GET['FinOrder']; $to = "email@gmail.com"; $subject = "New Order"; $message = " <html> <head> <title>New Order</title> </head> <body bgcolor='#000000' text='#FFFFFF'> <div align='center'> <img src='http://WEBSITE.com/cTrends/images/header.jpg' /> <br /> Client Name: ".$thisClient."<br /><br /> <table border='0' width='650'> <tr> <th width='100' align='center'>LCBO #</th> <th width='370' align='left'>Product</th> <th width='60' align='center'>Unit Price</th> <th width='60' align='center'>Qty</th> <th width='60' align='right'>Total</th> </tr>"; while($row = mysql_fetch_assoc($AllWine)){ $unit_price = "$".number_format(($row['wish_bottle'])/($row['wish_case']), 2); //$unit_price2 = ($unit_price)*($row['wish_case']); $message .= "<tr> <td align='center'>".$row['wish_bottle_LTO']."</td> <td align='left'>".$row['wish_client']."</td> <td align='center'>".$unit_price."</td> <td align='center'>".$row['wish_case']."</td> <td align='right'>$".number_format($row['wish_total'], 2)."</td> </tr>"; mysql_query("INSERT INTO recording_tbl (record_id, record_client, record_date, record_info, record_price) VALUES ('', '".$_GET['FinOrder']."', '".$today."', '".$row['wish_client']."', '".$_GET['Total']."')"); } $message .= "<tr> <td colspan='5' align='right'><b>Grand Total = $".number_format($grandTotal, 2)."</b></td> </tr></div></body></html>"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; // More headers $headers .= 'From: <email@gmail.com>'. "\r\n"; mail($to,$subject,$message,$headers); ?> It does work for both now. I'm not sure if that is what you meant entirely though... If there is any other suggestions to make it run better that would be great! It is working though now. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 You should never run queries in loops. You can make the insert in a loop one larger insert. Quote Link to comment Share on other sites More sharing options...
cdoggg94 Posted September 18, 2012 Author Share Posted September 18, 2012 I'm sure that's good advice, but I'm not really sure what you mean. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 See below Quote Link to comment Share on other sites More sharing options...
premiso Posted September 18, 2012 Share Posted September 18, 2012 And THENNNNNNN Quote Link to comment Share on other sites More sharing options...
cdoggg94 Posted September 18, 2012 Author Share Posted September 18, 2012 I will most definitely look into what you are saying in a couple hours. I have to go out for a while. Many thanks for your help and making me figure it out (somewhat) by myself...clearer clues then "see below" might be helpful in the future...but regardless, I appreciate your help. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 If you don't know what I mean by multi insert, then google it before asking how to do it. It's in the manual, which is the first hit for "mysql multi insert" and there are plenty of examples online. Quote Link to comment Share on other sites More sharing options...
premiso Posted September 18, 2012 Share Posted September 18, 2012 $insert = array(); while($row = mysql_fetch_assoc($AllWine)){ $unit_price = "$".number_format(($row['wish_bottle'])/($row['wish_case']), 2); //$unit_price2 = ($unit_price)*($row['wish_case']); $message .= "<tr> <td align='center'>".$row['wish_bottle_LTO']."</td> <td align='left'>".$row['wish_client']."</td> <td align='center'>".$unit_price."</td> <td align='center'>".$row['wish_case']."</td> <td align='right'>$".number_format($row['wish_total'], 2)."</td> </tr>"; $insert[] = "('', '".$_GET['FinOrder']."', '".$today."', '".$row['wish_client']."', '".$_GET['Total']."')"; } if (!empty($insert)) { mysql_query("INSERT INTO recording_tbl (record_id, record_client, record_date, record_info, record_price) VALUES " . implode(", ", $insert)); } That is what she meant. It is better and way more efficient to execute it all in one query (the syntax for that MySQL is called extended inserts). As long as I didn't make some weird syntax error that is Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 I would also remove record_id and the '' part, record_id should be an auto increment and you don't need to send any value Quote Link to comment 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.