试图在一封电子邮件中发送一组数据而不是多个

时间:2017-08-30 22:44:31

标签: php mysql mysqli

我有一个问题,当我提交4个订单的记录时,它会发送4封电子邮件,我需要帮助才能将所有项目放入一封电子邮件中,而不是每封邮件的电子邮件。我将继续努力弄清楚,帮助将不胜感激。我也知道代码需要写在一个准备好的语句中我将在我想出这个之后改变它。

.htaccess

1 个答案:

答案 0 :(得分:1)

您只需要在循环之外移动mail命令。相反,将所有消息编译成一个字符串,然后在最后发送消息。

例如,像这样:

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 = "xgrh@gmail.com";
    $subject = "Supplies";
    $headers = "From: user@gmail.com"; 

    mail($to,$subject,$message,$headers); 
}