发送电子邮件并将数据插入数据库php mysql
我已经为在表中插入数据创建了此表单发送电子邮件并将数据插入数据库php mysql,php,mysql,database,email,post,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
<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();
}
?>
$host=“localhost”;
$username=“root”;
$password=“”;
$db_name=“mydb”;
$tbl_name=“metbl”;
mysql_connect(“$host”、“$username”、“$password”)或die(“无法连接”);
mysql_select_db($db_name)或die(“无法选择db”);
如果(isset($_POST['enq_submit']))
{
$name=$_POST['co_name'];
$mobile=$_POST['co_mobile'];
$email=$_POST['co_email'];
$message=$\u POST['co\u message'];
$sql=“在$tbl_名称(fco_名称、fco_移动、fco_电子邮件、fco_文本)中插入值(“$name”、“$mobile”、“$email”、“$message”)”;
$result=mysql\u查询($sql);
如果($结果){
回音“OK”;
否则{
回声“错误”;
}
?>
1) 我想知道在插入数据库之后,如何发送一封包含字段条目的电子邮件。2)我还需要更新另一个表,但我不确定如何更新
$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);
要发送电子邮件,可以使用basic 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);
你的邮件就完了
你可以找到更多关于邮件功能的信息
至于更新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
希望这正是您想要的…也许可以将此作为几个问题发布。尝试将其分解为最少的示例,人们往往会更好地回答简短的问题。您好,谢谢您的回答。此代码可以通过localhost运行吗?@touinta我不使用localhost,但是如果您运行windows pc,除了从邮件功能。为此,您需要一个邮件传输代理,如sendmail。从未尝试过它,但请查看此处: