duanfu7840 2017-08-30 22:15
浏览 30
已采纳

收到要发送的电子邮件时遇到问题。 继续将Array转换为字符串转换

I am having an issue with the email portion of this code. when I submit the record it says inserted successfully inserted, but for the email I get Notice: Array to string conversion and it does not want to send the email of the products ordered to the email entered. I can not figure out what the issue is. I will keep trying different methods.

process_insert.php

 <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 = "test123@gmail.com";

    $subject = "Supplies";
    $headers = "From: user@gmail.com";  

    $message =

    "employee_name: " . $_POST['employee_name'] . " 

    " ."name: ".  $_POST['name'] ." 

    ". "code: " . $_POST['code'] . " 

    " ."quantity: ".  $_POST['quantity'] . " 

    ". "price: " . $_POST['price'] . "";


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


    ?>
    </body>
    </html>
  • 写回答

1条回答 默认 最新

  • dongliang1223 2017-08-30 22:24
    关注

    You have to put your email code inside loop like below:-

     <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 = "test123@gmail.com";
    
                    $subject = "Supplies";
                    $headers = "From: user@gmail.com";  
    
                    $message =
    
                    "employee_name: " . $employee_name . " 
    
                    " ."name: ".  $name ." 
    
                    ". "code: " . $code . " 
    
                    " ."quantity: ".  $quantity . " 
    
                    ". "price: " . $price . "";
    
    
                    mail($to,$subject,$message,$headers); 
                }
    
            }
        ?>
        </body>
        </html>
    

    Note:- You code is vulnerable to SQL INJECTION. Use prepared statements as @RiggsFolly suggested in comments

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?