Hi guys. I am now creating my sign up page and I would like to check if both name and email entered in page are already in the database.

So far I have

if(mysql_query("SELECT members FROM name WHERE $_POST['name'] = '$name' and email WHERE $_POST['email'] = '$email'" ))){

echo "Sorry! Your details are already in our database";

} else {
// code to insert values into databases.....

I have a problem with the top part. I refered $_POST as name entered as I gave it id name "name" on the form. When I run it it say programminn error.

Edited by london-G: mistake in title

5 Years
Discussion Span
Last Post by london-G
"SELECT `members` FROM `name` WHERE `name` = '$name' AND `email` WHERE `email` = '$email'"

I assume you've made $name and $email from $_POST and $_POST and have cleaned them with something like mysql_real_escape_string().

I also assume that the table name is 'name' and that you have a field called 'name' too.


Sorry for the confusion, I have the table named members and the fileds names email and name. I did made $name and $email from $_POST and $_POST.

My code is

if (mysql_query("SELECT * FROM members WHERE 'name' = '$name' AND 'email' = '$email'")){
			   echo "Sorry! Your details are already in our database";
			   } else {
// code to insert values into databases.....

Now it is only inserting the values of name and email into the database even though the database has the same values already.

$r = mysql_query("SELECT `name` FROM `members` WHERE `name` = '$name' AND `email` = '$email' LIMIT 1");
  //record found
  //record not found

You MUST use backticks for field names as opposed to normal single quotes. Also don't use * in your query as it's slow. Best to pick one field if you want to see if a record is present. If you use LIMIT 1 - the query stops searching the rest of the DB as soon as it gets one result - saving time.

The mysql_num_rows() is the standard way to check if a resource has any records in it. You could also use if(mysql_num_rows($r)){} or if(!mysql_num_rows($r)){}, but I think the one I supplied illustrates the use.

Votes + Comments
thanks for the advise!

Thanks for the advise. I did change my code now but still not working. I have posted my full code now

// table name


$exist = mysql_query("SELECT 'name' FROM members WHERE 'name' = '$name' AND 'email' = '$email'")

if (mysql_num_rows($exist)>0) {
			   echo "Sorry! Your details are already in our database";
			   } else {

// Insert data into database
$sql = "INSERT INTO $tbl_name VALUES('$name','$email')";

// if suceesfully
echo "Success!";

Edited by london-G: n/a


Where's your connection details? You don't seem to have connected to a DB.


Sorry I forgot to post the connection bit. I did changed


but still doesn't work.

It keeps saying success (adds to database too) even though I have inserted the same name and address thousand times already :(.

Edited by london-G: n/a


you are still not using backticks as ardav told you to
` is diverend than '
` is on the same key as the ~ ( atleast on my keyboard)

or you can copy and past ardav code


I really feel stupid lol. Sorry ardav I missed your point here. It works now. Thank you so much!

Edited by london-G: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.