[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP mail send once with multiple rows being inserted

Posted on 2011-10-10
1
Medium Priority
?
182 Views
Last Modified: 2012-05-12
I am inserting the data from an array into multiple rows of my database. That works fine.  

I also have it sending an email upon success.  Only problem is that it is sending an email for each row it is inserting. How can i just limit it to send one email if the input is successful?

 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$count=mysql_num_rows($rsDesignated);
$acctID = $_POST['acctID'];
$amount = $_POST['amount'];
for($i=0; $i<$count; $i++){
mysql_select_db($database_con_portal_test, $con_portal_test);
  $SQL = "INSERT INTO tbldepbrk (depBrkAmnt, depBrkDate, depDesAcctID, depBrkClientID ) VALUES ('$amount[$i]' , '$_POST[date]' , '$acctID[$i]' , '$row_rsDesignated[desAcctGrpID]')";
   
   if (!mysql_query($SQL, $con_portal_test))
  {
  echo ('Error: ' . mysql_error($con_portal_test));
  }
   if ($SQL) {
  //  email confirmation to axess admins 
			   
			   
			   // send e-mail to ...
			$to = "info@thechurchbusinessguys.com";
			$to2 = $row_rs_user['usrEmail']; 
			
			// Your subject
			$subject= "Deposit Info Submitted by:" .$_SESSION['MM_Username'];  
			$subject2 = "Your Deposit information has been submitted"; 
			
			// From
			$header="Axess Client Portal Messaging";
			
			// Your message
			
			$messages = "a client has submitted a new deposit to be recorded.";
			$message2 = "Thanks for your submission.  One of our staff members will process the information shortly.  You will receive an email once the payment has been processed in our system.";
			
			
			// send email
			$sentmail = mail($to, $header, $subject, $messages);
			$sentmail2 = mail($to2, $header, $subject2, $message2);                
}
}}
?>

Open in new window


I know its sending multiple because i have it set if($SQL) and that happens each time a row is inserted.  How can i limit it?
0
Comment
Question by:axessJosh
1 Comment
 
LVL 7

Accepted Solution

by:
amigura earned 1000 total points
ID: 36944426
you had email in for loop thats why
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$count=mysql_num_rows($rsDesignated);
$acctID = $_POST['acctID'];
$amount = $_POST['amount'];
for($i=0; $i<$count; $i++){
mysql_select_db($database_con_portal_test, $con_portal_test);
  $SQL = "INSERT INTO tbldepbrk (depBrkAmnt, depBrkDate, depDesAcctID, depBrkClientID ) VALUES ('$amount[$i]' , '$_POST[date]' , '$acctID[$i]' , '$row_rsDesignated[desAcctGrpID]')";
   
   if (!mysql_query($SQL, $con_portal_test))
  {
  echo ('Error: ' . mysql_error($con_portal_test));
  }
   if ($SQL) {
  //  count inserts
		$db_good+=1;	               
}

}


if ($db_good>0) {
  //  email confirmation to axess admins 
			   
			   
			   // send e-mail to ...
			$to = "info@thechurchbusinessguys.com";
			$to2 = $row_rs_user['usrEmail']; 
			
			// Your subject
			$subject= "Deposit Info Submitted by:" .$_SESSION['MM_Username'];  
			$subject2 = "Your Deposit information has been submitted"; 
			
			// From
			$header="Axess Client Portal Messaging";
			
			// Your message
			
			$messages = "a client has submitted a new deposit to be recorded.";
			$message2 = "Thanks for your submission.  One of our staff members will process the information shortly.  You will receive an email once the payment has been processed in our system.";
			
			
			// send email
			$sentmail = mail($to, $header, $subject, $messages);
			$sentmail2 = mail($to2, $header, $subject2, $message2);                
}

}
?>

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question