Jump to content

Email & Insert


cdoggg94

Recommended Posts

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...:(

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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 :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.