PHP mail send once with multiple rows being inserted

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?
LVL 2
axessJoshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amiguraCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.