Karlos
Karlos

Reputation: 1

email sending on php form registration

Can't seem to get thhis script work. Although registration part works fine and data gets inserted but shows error in result page:

Not found your email in our databaseCannot send Confirmation link to your e-mail address

Code:

<?php
$con=mysqli_connect("localhost","","");
if (!$con) { die("Database connection failed: " . mysqli_error());}
$db_select=mysqli_select_db($con, "");
if (!$db_select) { die("Database selection failed: " . mysqli_error());}

if (isset($_POST['register'])) {
$username = mysqli_real_escape_string($con,$_POST['user']);
$password1 = mysqli_real_escape_string($con,$_POST['pass1']);
$password2 = mysqli_real_escape_string($con,$_POST['pass2']);
$name = mysqli_real_escape_string($con,$_POST['name']);
$phone = mysqli_real_escape_string($con,$_POST['phone']);
$email = mysqli_real_escape_string($con,$_POST['email1']);
if(isset($_POST['country'])){ $country = $_POST['country'];}
if(isset($_POST['state'])){ $state = $_POST['state'];}
// Random confirmation code
$confirm_code=md5(uniqid(rand())); 

/* Data Insertion & PASSWORD 1 = 2 CHECK */
if($password1==$password2 && !empty($username) && !empty($name) && !empty($phone) && !empty($email) && !empty($country) && !empty($state)){ /* IF PASSWORD1 IS THE SAME WITH PASSWORD2 */
/* INSERT QUERY */    
$query = "INSERT INTO temp_users (confirm_code,username,password,name,phone,email,country,state) VALUES ('$confirm_code','$username','$password1','$name','$phone','$email','".$country."','".$state."')";
$result=mysqli_query($con, $query);
if ($result) {
$to = $email;
$subject = "Activate Vacation Rental Link";
// From
$header = "VacationByChoice <[email protected]>";
// message
$message = "Your Confirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="https://craigburst.com/confirmation-link-validation.php?passkey=$confirm_code";
// send email
if(!mail($to, $subject, $message, $header)) {
echo "Cannot send Confirmation link to your e-mail address";
} else {
echo "Your Confirmation link Has Been Sent To Your Email Address.";}
} else {
echo "Not found your email in our database";}}}
?>

Form:

Username:<input type="text" name="user" maxlength="20" class="username" value="<?php echo $username; ?>" /><?php if (!empty($username) === false){echo '<font style="color:#FF0000">* Username</font>';
} else /* duplicate username Validation */ $check_user = ("SELECT username FROM temp_users WHERE username='$username'"); $run = mysqli_query($con,$check_user);
if(mysqli_num_rows($run)>0) {
echo "<font style='color:#FF0000'>* User Exists</font>";} ?>

Choose Password:<input type="password" maxlength="20" name="pass1" value="<?php echo $password1; ?>" /><?php if (!empty($password1) === false){
echo '<font style="color:#FF0000">* Password</font>';} ?>

Repeat Password:<input type="password" maxlength="20" name="pass2" value="<?php echo $password2; ?>" /><?php if (!empty($password2) === false){
echo '<font style="color:#FF0000">* Password</font>';}
elseif($password1!=$password2) { echo '<font style="color:#FF0000">* No Match</font>';} ?>

Full Name:<input type="text" maxlength="50" name="name" value="<?php echo $name; ?>" /><?php if (!empty($name) === false){
echo '<font style="color:#FF0000">* Name</font>';} ?>

Phone:<input type="text" maxlength="20" name="phone" value="<?php echo $phone; ?>" /><?php if (!empty($phone) === false){
echo '<font style="color:#FF0000">* Phone</font>';} ?>

Email:<input type="text" maxlength="50" name="email1" value="<?php echo $email; ?>" /><?php if (!empty($email) === false){
echo '<font style="color:#FF0000">* Email</font>';}
elseif (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
echo '<font style="color:#FF0000">* email';} else
/* duplicate Entry Validation */
$check_email = "SELECT email FROM temp_users WHERE email='$email'";
$run = mysqli_query($con,$check_email);
if(mysqli_num_rows($run)>0) {
echo "<font style='color:#FF0000'>* Email Exists</font><br/>";} ?>

Country:<select id="country" class="style" name="country">
<option>Select</option></select><?php if ($country=['select']){
echo '<font style="color:#FF0000">* Country</font>';} ?>

State/ Province:<select id="state" class="style" name="state"></select><?php if ($state=['select']){
echo '<font style="color:#FF0000">* State</font>';} ?>

<img src="captcha.php" alt="Captcha" /><input type="text" name="vercode" /><?php /* Captcha Validation */
if ($_POST['vercode']!=$_SESSION['vercode'] OR $_SESSION["vercode"]===''){
echo '<font style="color:#FF0000">* code</font>';} ?>

<input type="submit" name="register" value="Sign Up" />

Seeking help on this. Error reporting has been added already.

Upvotes: 0

Views: 169

Answers (1)

Darren
Darren

Reputation: 13128

As my brother from another mother has suggested. You can try this.

Firstly set error reports on to help you debug this further.

ini_set('display_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

That should help you narrow down the cause of your errors. Now on to your query. As you can see, you're setting your query like this:

$query = mysqli_query($con,"INSERT INTO temp_users (confirm_code,username,password,name,phone,email,country,state) VALUES ('$confirm_code','$username','$password1','$name','$phone','$email','$country','$state')");
$result=mysqli_query($query);

Which means, you're trying to run it like this: mysqli_query(mysqli_query('sql query in here')). That won't work. Try setting it up like this:

$query = "INSERT INTO temp_users (confirm_code,username,password,name,phone,email,country,state) VALUES ('$confirm_code','$username','$password1','$name','$phone','$email','$country','$state')";
$result=mysqli_query($con, $query);



You see how $query is the actual query string and $result runs the query through mysqli_query().

And now keeping in scope, you should do something like this with your code blocks for better management:

if ($result) {
            $to = $email;
            $subject = "Avtivate Vacation Rental Link";
            // From
            $header = "VacationByChoice <[email protected]>";
            // message
            $message = "Your Comfirmation link \r\n";
            $message.="Click on this link to activate your account \r\n";
            $message.="https://craigburst.com/confirmation-link-validation.php?passkey=$confirm_code";
            // send email
            if(!mail($to, $subject, $message, $header)) {
                echo "Cannot send Confirmation link to your e-mail address";
            } else {
                echo "Your Confirmation link Has Been Sent To Your Email Address.";
            }
        } else {
            echo "Not found your email in our database";
        }

Notice how we removed the whole $sentmail and check mail directly with the if statement.

Does that help you out?


Notes

Just a few side notes; you have a typo here:

$subject="Avtivate Vacation Rental Link";

should be

$subject="Activate Vacation Rental Link";

and

$message="Your Comfirmation link \r\n";

should be

$message="Your Confirmation link \r\n";




Just had to be picky or Fred will beat me:P

Upvotes: 1

Related Questions