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.

Recommended Answers

All 8 Replies

Member Avatar for diafol
"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.

Member Avatar for diafol
$r = mysql_query("SELECT `name` FROM `members` WHERE `name` = '$name' AND `email` = '$email' LIMIT 1");
if(mysql_num_rows($r)>0){
  //record found
}else{
  //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.

commented: thanks for the advise! +2

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

// table name
$tbl_name="members";

$name=$_POST['name'];
$email=$_POST['email'];


$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')";
			  
			   
$result=mysql_query($sql);
 }

// if suceesfully
if($result){
echo "Success!";
}
Member Avatar for diafol
$name=mysql_real_escape_string($_POST['name']);
$email=mysql_real_escape_string($_POST['email']);

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

$name=mysql_real_escape_string($_POST['name']);$email=mysql_real_escape_string($_POST['email']);

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 :(.

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.