Faqts : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 6 people (83%) answered Yes
Recently 3 of 4 people (75%) answered Yes

Entry

How can I compare form input (e.g., email) with existing database entries (existing email)?

Oct 16th, 2002 09:46
Alexander Erazo, Bill Seaton,


<?
// you can do something like this. Note this file should be
// called in the <form action .... tag
// script processform.php
// returns 1 if email exists, 0 otherwise
function chk_email_exists($email="")
{
	$tablename = "thetable";
	if (!$email)
	{
		die("Function Call Syntax Error: 
chk_email_exists(string email)");
	}
	$sql = "select email from $tablename where email = $email";
	$result = mysql_query($sql) or die(mysql_error());
	if (mysql_num_rows($result))
	{
		return 1;
	}
	return 0;
}
// in main
// check for email exists
if (chk_email_exists($email))
{
	echo "Sorry! The email already exists. Click Back and try 
another email\n";
}
else
{
	// continue inserting
	//....
}
?>