发送电子邮件并将数据插入数据库php mysql

时间:2014-12-11 15:42:51

标签: php mysql database email post

我已经为表格中的插入数据创建了此表单

<form class="form" method="post" action="myactionpage.php" enctype="multipart/form-data">
<p>
<label>Name *</label>
<input type="text" name="co_name" placeholder="Full Name" required />
</p>
<p>
<label>Mobile *</label>
<input type="number" name="co_mobile" required />
</p>
<p>
<label>Email *</label>
<input type="email" name="co_email" required />
</p>
<p>
<label>Message</label>
<textarea rows="5" name="co_message" cols="15"></textarea>
</p>
<input type="submit" name="enq_submit" value="Apply Online"  />
</form>

和我的动作php页面

$host="localhost"; 
$username="root"; 
$password=""; 
$db_name="mydb"; 
$tbl_name="metbl";


mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['enq_submit']))
{ 
$name=$_POST['co_name'];
$mobile=$_POST['co_mobile'];
$email=$_POST['co_email'];
$message=$_POST['co_message'];
$sql="INSERT INTO $tbl_name(fco_name, fco_mobile, fco_email,fco_text)VALUES('$name',                                                     '$mobile', '$email','$message' )";
$result=mysql_query($sql);

if($result){
echo "OK";

else {
echo "ERROR";
}
?> 

<?php 

mysql_close();
}
?>

1)我想知道插入数据库后如何发送包含字段条目的电子邮件。 2)我还需要更新另一个表,但我不知道该怎么做

2 个答案:

答案 0 :(得分:4)

要发送电子邮件,您可以使用基本的mail()php函数。

首先,定义标题:

$headers = "From: myplace@yourdomain.com\r\n";
$headers .= "Reply-To: myplace2@yourdomain.com\r\n";
$headers .= "Return-Path: myplace@yourdomain.com\r\n";
$headers .= "CC: sombodyelse@anotherdomain.com\r\n";
$headers .= "BCC: hidden@anotherdomain.com\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();

之后,将主题行放在变量中以便于使用:

$subject = "My example email subject";

接下来,您将对电子邮件正文执行相同的操作:

$body = "This is the body of the email.\n\n";
$body .= "The name is: ".$name."\n";
$body .= "The mobile is: ".$mobile."\n";
$body .= "The email is: ".$email."\n";
$body .= "The message is: ".$message."\n";
$body .= "This is the end of the message";

最后,指定收件人:

$to = "recipient@yourdomain.com";

将其包装成邮件功能:

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

你完成了邮件!

有关邮件功能的更多信息,请查看here

至于更新mysql,你可以这样做:

首先确定如何识别表格。这可以通过例如行id:

来完成
$id = "3"; //3 is hypothetical

然后运行查询:

mysql_query=("UPDATE other_tbl_name SET fco_name = '$name', fco_mobile = '$mobile', fco_email = '$email', fco_text = '$message' WHERE id = '$id'"); //here we used the row's id to specify which row to update

希望这就是你要找的......

答案 1 :(得分:2)

  1. 要发送电子邮件,您可以使用:

        $to      = 'xxx@yyy.zzz';
        $subject = 'email title';
    
        $headers = 'From: zzz@yyy.xxx' . "\r\n" .
            'Reply-To: zzz@yyy.xxx' . "\r\n" .
            "MIME-Version: 1.0\r\n".
            'Content-Type: text/html; charset=ISO-8859-1\r\n'."\r\n" .
            'X-Mailer: PHP/' . phpversion();
    
    
        $message= 'your message goes here';
    
        mail($to, $subject, $message, $headers);
    
  2. 您需要先替换一些数据。

    1. 你可以像第1次一样进行第二次插入($ sql =“INSERT INTO tbl_name(fco_name,fco_mobile,fco_email,fco_text)VALUES('$ name','$ mobile','$ email', '$ message')“;)...只需更改表名,列和值。