Jump to content

Checking if email is already used


Dren

Recommended Posts

Hi, i have a problem with my code...

I was dealing with registration and validation forms... and I encountered this problem.

When i enter an e-mail my code doesn't recognize it as already in use... and adds another user with the same email.
It doesn' say any error, just adds the new user with the same email in the table.

 

here my code:

	<?php
		include ("phpconnect.php");

		mysqli_select_db($connect, $database);
		echo "connected to $database<br>";

		$name = $_POST['user'];
		$surname = $_POST['surname'];
		$password = $_POST['password'];
		$email = $_POST['email'];
		$reemail = $_POST['email'];

		$check = mysqli_query($connect, "SELECT * FROM users WHERE email = '.$email.'");

		if(mysqli_num_rows($check) > 0){
			echo "The email is already in use!";
		}else{
			mysqli_query($connect, "INSERT INTO users (name, surname, password, email) VALUES ('$name', '$surname', '$password', '$email')");
			echo "added $email";

			mysqli_close($connect);
		}
	?>

I tried everything by looking on google ... 

Link to comment
https://forums.phpfreaks.com/topic/283083-checking-if-email-is-already-used/
Share on other sites

Change this line:

$check = mysqli_query($connect, "SELECT * FROM users WHERE email = '.$email.'");

for that:

$check = mysqli_query($connect, "SELECT * FROM users WHERE email = '$email'");

In the other hand, look, you have problems with security...

Please, read about: SQL Injection (search in google)

 

You need use this funcion: mysqli_real_escape_string().

Read about that quickly

 

Change this line:

$check = mysqli_query($connect, "SELECT * FROM users WHERE email = '.$email.'");

for that:

$check = mysqli_query($connect, "SELECT * FROM users WHERE email = '$email'");

 

oh my ... ._." i tried the IMPOSSIBLE and the error was just that "."... thank you  :

It works now!

 

This code is just a game for me to learn better php... I'll write the security soon!

The error is in SQL, not in PHP.

 

The QUERY executed are:

SELECT * FROM users WHERE email = '.BrodaNoel.'

And... .BrodaNoel. is not my username...

If you whant use "." the code should be:

$check = mysqli_query($connect, "SELECT * FROM users WHERE email = '".$email."'");

 

The error is in SQL, not in PHP.

 

The QUERY executed are:

SELECT * FROM users WHERE email = '.BrodaNoel.'

And... .BrodaNoel. is not my username...

If you whant use "." the code should be:

$check = mysqli_query($connect, "SELECT * FROM users WHERE email = '".$email."'");

 

Oh thanks for your help :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.