Reputation: 699
I have an issue where when I submit the record with 4 orders it sends 4 emails I need assistance in getting the items to all go in one email not an email for each individual item. I will keep trying to figure it out and help would be appreciated. I also know the code needs to be written in a prepared statement I will be changing it after I figure this out.
<html>
<head>
<title></title>
</head>
<body>
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$serverName = "localhost";
$userName = "root";
$userPassword = "";
$dbName = "blog_samples";
$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
$rows_count = count($_POST["name"]);
for($i=0;$i<$rows_count;$i++){
// PREVENTING SQL INJECTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$employee_name = mysqli_real_escape_string($conn,$_POST["employee_name"][$i]);
$name = mysqli_real_escape_string($conn,$_POST["name"][$i]);
$code = mysqli_real_escape_string($conn,$_POST["code"][$i]);
$quantity = intval($_POST["quantity"][$i]);
$price = mysqli_real_escape_string($conn,$_POST["price"][$i]);
$sql = "INSERT INTO order_table ( employee_name, name, code, quantity, price)
VALUES ('$employee_name', '$name', '$code', '$quantity', '$price')";
$query = mysqli_query($conn,$sql);
if(mysqli_affected_rows($conn)>0) {
echo "Record add successfully";
$to = "[email protected]";
$subject = "Supplies";
$headers = "From: [email protected]";
$message =
"employee_name: " . $employee_name . "
" ."name: ". $name ."
". "code: " . $code . "
" ."quantity: ". $quantity . "
". "price: " . $price . "";
mail($to,$subject,$message,$headers);
}
}
?>
</body>
</html>
Upvotes: 0
Views: 27
Reputation: 1646
You just need to move the mail
command outside of your loop. Instead, compile all the messages into one string then send the message at the end.
For example, something like this:
ini_set('display_errors', 1);
error_reporting(~0);
$serverName = "localhost";
$userName = "root";
$userPassword = "";
$dbName = "blog_samples";
$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
$rows_count = count($_POST["name"]);
$message = '';
for($i=0;$i<$rows_count;$i++){
// PREVENTING SQL INJECTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$employee_name = mysqli_real_escape_string($conn,$_POST["employee_name"][$i]);
$name = mysqli_real_escape_string($conn,$_POST["name"][$i]);
$code = mysqli_real_escape_string($conn,$_POST["code"][$i]);
$quantity = intval($_POST["quantity"][$i]);
$price = mysqli_real_escape_string($conn,$_POST["price"][$i]);
$sql = "INSERT INTO order_table ( employee_name, name, code, quantity, price)
VALUES ('$employee_name', '$name', '$code', '$quantity', '$price')";
$query = mysqli_query($conn,$sql);
if(mysqli_affected_rows($conn)>0) {
echo "Record add successfully";
$message .=
"employee_name: " . $employee_name . "
" ."name: ". $name ."
". "code: " . $code . "
" ."quantity: ". $quantity . "
". "price: " . $price . "";
}
}
if ( ! empty($message)) {
$to = "[email protected]";
$subject = "Supplies";
$headers = "From: [email protected]";
mail($to,$subject,$message,$headers);
}
Upvotes: 1