-1

I'm trying to send mail with Id Number for user registering, id number inserted automatic from MySQL...

The id number is a primary key and auto increment. Its name in MySQL is 'con_id'

I tried this code:

This is a register.php code and sendmail function:

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

An email was successfully sent for a new user registered, but not the id number found or not found. Any content email is empty.

Note: The user registered will be logged in with id number only...

How can I fix this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Azer
  • 3
  • 1
  • 7
  • Sending email to an arbitrary address with an arbitrary body is just asking for someone to abuse this feature. – tadman Jun 23 '16 at 15:24
  • There is not right way but you can use it. first you make insert query then after make select query where make ordery by desc id so you get the last id then send this id in email. – Yagnik Detroja Jun 23 '16 at 15:31
  • When the user are registering process are very frequently then issue arise – Yagnik Detroja Jun 23 '16 at 15:34

3 Answers3

1

You are trying to get the con_id from $_[POST] but that's not possible since there is no id for the person yet. Of course it will be empty. You need to retrieve the newly created id from the consumer table.

To do this in mysql_ functionality you will need to execute the query BEFORE sending the email, then use mysql_insert_id() to get the id that was inserted when you ran $query

That being said:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
JNevill
  • 46,980
  • 4
  • 38
  • 63
-1

It's all simple you can get the last inserted id and send it as a parameter to the link: here is how you will proceed:

In the registerUser function

}
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;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PacMan
  • 1,358
  • 2
  • 12
  • 25
-1

This is the answer and worked correctly:

$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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Azer
  • 3
  • 1
  • 7
  • That can't possible be working correctly. The `$message` string isn't terminated, so that would end up with a PHP error. Also note that `$body` isn't used in this code, even though it's set to `$_POST["con_name"]` - did you intend to concatenate `$content` to `$body` to provide a readable email body? – Michael Gaskill Jun 24 '16 at 01:22