基于使用php的mysql查询的自动回复电子邮件

时间:2017-09-23 16:19:50

标签: php mysql email

我正在处理一个简单的表单,只有2个字段。姓名和电子邮件。我到目前为止所取得的成就是。

  1. 我将表单发送到mysql数据库。
  2. 我可以总共查询数据库,也可以根据表单条目查询数据库。
  3. 我已经过测试,我可以使用基本邮件脚本或表单发送电子邮件。
  4. 我遇到问题的地方是我需要将数据与数据库中的第三个字段一起发送给提交者,这是自动增量主键作为其成员编号。我的问题是如何根据数据库条目发送电子邮件。

    即;感谢您加入会员ID的“提交者姓名”是“主要密钥”。 这是我目前的代码。

    的index.html

    <form method="post" action="doit.php">
        Name<input name="Name" type="text" /><br>
        Email<input name="Email" type="text" /><br>
        <input name="Submit" type="submit" value="submit" />
    </form>
    

    doit.php

    <?php
    $con = mysqli_connect('localhost','DB_Username','DB_Password');
    
    if(!$con)
    {
        echo 'Not Connected To Server';
    }
    if(!mysqli_select_db($con,'DB_Name'))
    {
        echo 'Database Not Selected';
    }
    $Name = $_POST['Name'];
    $Email = $_POST['Email'];
    
    $sql = "INSERT INTO Table_name (Name,Email) VALUES ('$Name','$Email')";
    
    if(!mysqli_query($con,$sql))
    {
        echo 'Not Inserted';
    }
    else
    {
        echo 'Inserted';
    }
    header ("refresh:2; url=auto-response.php");
    
    
    ?>
    

    自动response.php

    <?php
    if ($_SERVER["REQUEST_METHOD"}=="POST") {
        $connnection=mysqli_connect("localhost","DB_username","DB_password","DB_Name");
        // Check connection
        if ($connection) {
            echo "connection successfull! <br>;"
        } else {
            die("connection failed. Reason ". mysqli_connect_err())
    }
    {
    
    $Email=$_POST["Email"];
    $Name=$_POST["Name"];
    $ID=$_POST["ID"];
    
    $sql="SELECT ID,Email,Name FROM Table_Name WHERE Email='".$Email."'";
    $result = mysqli_query($connection,$sql);
    
    $to      = 'Email';
    $subject = 'Thank you For Joining';
    $message = 'Thank you for joining "Email" Your Member ID is "ID"';
    $headers = 'From: dsarmy@thedarksideshop.com' . "\r\n" .
        'Reply-To: dsarmy@thedarksideshop.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    ?> 
    

1 个答案:

答案 0 :(得分:0)

Mysql有自己的功能,可以给你id。虽然您在插入时已经有了名称。

// Using the below line right after your insert you can get the primary id.
$id = $connection->insert_id; // Get latest inserted id.

希望它有所帮助。

编辑:根据您所做的编辑,我已将您的代码安排到应该如何运作。现在试试这个。

<强>的index.html

<form method="post" action="doit.php">
    Name<input name="Name" type="text" /><br>
    Email<input name="Email" type="text" /><br>
    <input name="Submit" type="submit" value="submit" />
</form>

<强> doit.php

$con = mysqli_connect('localhost','DB_Username','DB_Password');

if(!$con)
{
    echo 'Not Connected To Server';
}
if(!mysqli_select_db($con,'DB_Name'))
{
    echo 'Database Not Selected';
}

$Name = $_POST['Name'];
$Email = $_POST['Email'];

$sql = "INSERT INTO Table_name (Name,Email) VALUES ('$Name','$Email')";

if(!mysqli_query($con,$sql))
{
    echo 'Not Inserted';
}
else
{
    echo 'Inserted';
    $id = $con->insert_id; // Get latest inserted id.

    $to      = $Email;
    $subject = 'Thank you For Joining';
    $message = 'Thank you for joining "'.$Name.'" Your Member ID is "'.$id.'"';
    $headers = 'From: dsarmy@thedarksideshop.com' . "\r\n" .
        'Reply-To: dsarmy@thedarksideshop.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

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