如何为在PHP注册的新用户发送带有ID号的邮件

时间:2016-06-23 14:56:29

标签: 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;
       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();
       }
    }
?>

已成功为已注册的新用户发送了一封电子邮件,但未找到或未找到的ID号。任何内容电子邮件都是空的。

注意:注册的用户将仅使用ID号登录...

如何解决此问题?

3 个答案:

答案 0 :(得分:1)

您正试图从con_id获取$_[POST],但这是不可能的,因为此人尚无身份证明。当然它会是空的。您需要从id表中检索新创建的consumer

要在mysql_功能中执行此操作,您需要在发送电子邮件之前执行查询,然后使用mysql_insert_id()获取您在运行{{1}时插入的id }

话虽如此:

  

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分:0)

这很简单,您可以获取最后插入的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;

答案 2 :(得分:0)

这是答案并正确运作:

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