如何为在PHP中注册的新用户发送带有ID号的邮件
我正在尝试发送带有用户注册Id号的邮件,自动从MySQL插入Id号 id号是一个主键并自动递增。它在MySQL中的名称是“con_id” 我尝试了以下代码: 这是register.php代码和sendmail函数:如何为在PHP中注册的新用户发送带有ID号的邮件,php,mysql,Php,Mysql,我正在尝试发送带有用户注册Id号的邮件,自动从MySQL插入Id号 id号是一个主键并自动递增。它在MySQL中的名称是“con_id” 我尝试了以下代码: 这是register.php代码和sendmail函数: <?php class register { private $con_name; private $con_email; private $con_cell; private $con_password;
<?php
class register
{
private $con_name;
private $con_email;
private $con_cell;
private $con_password;
private $cxn;
function __construct($data)
{
if (is_array($data)) {
$this->setData($data);
}
else {
throw new Exception("register not found");
}
$this->connectToDb();
$this->registerUser();
}
private function setData($data)
{
$this->con_name = $data['con_name'];
$this->con_email = $data['con_email'];
$this->con_cell = $data['con_cell'];
$this->con_password = $data['con_password'];
}
private function connectToDb()
{
include '../models/database.php';
$vars = '../include/vars.php';
$this->cxn = new database($vars);
}
function registerUser()
{
$query = mysql_query("SELECT con_email FROM consumer WHERE con_email='$this->con_email'");
if (mysql_num_rows($query) > 0) {
header('location:../invalid-consumer.php');
}
else {
$query = "INSERT INTO `consumer`(`con_id`, `con_name`, `con_email`, `con_cell`, `con_password`) VALUES ('', '$this->con_name', '$this->con_email',
'$this->con_cell', '$this->con_password' )";
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" . "activate.php?id=" . $current_id;
$toEmail = $_POST["con_email"];
$body = $_POST["con_name"];
$subject = "User Registration Activation Email";
$content = $_POST["con_id"];
$mailHeaders = "From: Admin\r\n";
if (mail($toEmail, $subject, $content, $mailHeaders)) {
$message = "You have registered and the activation mail is sent to your email. Click the activation link to activate you account.";
}
unset($_POST);
$sql = mysql_query($query);
if ($sql)
header('location:../success.php');
else {
throw new Exception("Error not registerd");
}
}
}
function close()
{
$this->cxn->close();
}
}
?>
您正试图从$[POST]
获取con\u id
,但这是不可能的,因为还没有此人的id。当然是空的。您需要从consumer
表中检索新创建的id
要在mysql\u
功能中执行此操作,您需要在发送电子邮件之前执行查询,然后使用mysql\u insert\u id()
获取运行$query
时插入的id
也就是说:
。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO
很简单,您可以获取最后插入的id并将其作为参数发送到链接:下面是您将如何继续:
在registerUser函数中
}
else {
$query = "INSERT INTO `consumer`(`con_id`, `con_name`, `con_email`,
`con_cell`, `con_password`) VALUES ('', '$this->con_name', '$this->con_email', '$this->con_cell', '$this->con_password' )";
// Here you get the last inserted id and send it as an email parameter
$current_id = mysql_insert_id();
// And now you can send it correctly to the user
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" . "activate.php?id=" . $current_id;
这是正确的答案,工作正常:
$query ="INSERT INTO `consumer`(`con_id`, `con_name`, `con_email`, `con_cell`, `con_password`) VALUES ('', '$this->con_name', '$this->con_email', '$this->con_cell', '$this->con_password' )";
$sql = mysql_query($query);
$con_id = mysql_insert_id();
$toEmail = $_POST["con_email"];
$body = $_POST["con_name"];
$subject = "User Registration Activation Email";
$content = $con_id;
$mailHeaders = "From: Admin\r\n";
if (mail($toEmail, $subject, $content, $mailHeaders)) {
$message = "You have registered and the activation mail is sent to your
}
unset($_POST);
用任意的正文向任意地址发送电子邮件只是要求某人滥用此功能。没有正确的方法,但你可以使用它。首先进行插入查询,然后进行选择查询,其中按描述id进行订购,这样您就可以获得最后一个id,然后通过电子邮件发送此id。当用户注册过程非常频繁时,请发送arisehi,感谢回复,我尝试了以下代码:成功地发送了邮件,但在邮件中写入0个数字,而用户不是0个数字。您能将我在代码中写入的内容实际写入我吗。。else{$con_id=mysql_insert_id();$toEmail=$_POST[“con_email”];$body=$_POST[“con_name”];$subject=“User id”;$content=$con_id;$mailHeaders=“From:Admin\r\n”;如果(mail($toEmail,$subject,$content,$mailHeaders)){$message=“您已注册,id邮件已发送到您的电子邮件。”;}取消设置($(POST);我已经有好几年没有写过PHP了。我可能不是为你编写代码的最佳人选。这里最大的收获是将$sql=mysql\u查询($query);
移到你的电子邮件生成逻辑之上。然后在$sql=mysql\u查询($query)之后粘贴类似$new\u id=mysql\u insert\id();
的内容
行。然后使用$new\u id
而不是$\u POST[con\u id]
。非常感谢JNevill,它已经工作并发送了新用户Thankst的id,但无法正常工作。$message
字符串没有终止,因此最终会出现PHP错误。还要注意,$body
未在该代码中使用,即使它设置为$\u POST[“con\u name”]
-您是否打算将$content
连接到$body
以提供可读的电子邮件正文?