doukuanjing5404 2017-09-23 16:19
浏览 80
已采纳

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

I am working on a simple form only 2 fields. Name and Email. What I have succeded in doing so far is.

  1. I have the form send the data to a mysql database.
  2. I can query the database in total and also query the database based on a form entry.
  3. I have tested and I can send email using a basic mail script or from a form.

Where I am running to an issue is I need the data to also be sent to the submitter along with a third field from the database whish is the auto-encrement primary key as their member number. My question is how can I send email based on the database entry.

ie; Thank you "Submitter Name" for joining your Member ID is "Primary Key". This is my current code.

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");


?>

auto-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' . "
" .
    'Reply-To: dsarmy@thedarksideshop.com' . "
" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?> 
  • 写回答

1条回答 默认 最新

  • donte1234567 2017-09-23 16:38
    关注

    Mysql has its own function which will give you id. While you already have the name at the time of insert.

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

    Hope it helps.

    Edit: Based on the edit you made i have arranged your code to how it should work. Try this now.

    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' . "
    " .
            'Reply-To: dsarmy@thedarksideshop.com' . "
    " .
            'X-Mailer: PHP/' . phpversion();
    
        mail($to, $subject, $message, $headers);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分