Peter Azer Peter Azer - 5 months ago 13
SQL Question

how to send mail with ID number for new user registered in php

hello im trying to send mail with Id Number for user registering, id number inserted automatic from mysql ...

id number is a primary key and auto increment, its name in mysql '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();
}
}
?>


email succesfully sent for new user rigesterd but not id number found or not found any content email is empty,

Note: the user registered will be login with id number only..

please i need help,

best regards,

thank you very much..

Answer

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.