PDA

View Full Version : How can I check if email exists in db and if so, give error?


kymberm
10-2-03, 11:12 AM
I'm writing a survey, trying to enter data from a form into my database. I want to set it up so people can only enter the survey once and when they submit it i'll email a thank you. My code isnt' working, I can enter data into the database, but I added a conditional to check for if the email exists and if so, tell user bye:

// conect to my mysql database
$con = mysql_connect("sql02.mysite.com", "myuser", "mypass")or die("Connect Error: ".mysql_error());
$db = ("grp");
mysql_select_db($db, $con) or die (mysql_error());
//check for previous entry keyed to email
$chkEmail = mysql_query("SELECT * from survey where Email = '.$Email.'", $con) or die ("cannot get Database open");
if ($chkEmail)
{
printf ("<center><h2>You have already done this survey");
printf ("<center><h2>thanks!");
exit;
}
// insert data into the database grp
$myquery = "INSERT INTO survey (Email, Q1, Q2) VALUES
('".$Email."', '".$Q1."','".$Q2."')";

$result = mysql_query($myquery);
if ($result)
{
//Once the data is entered, redirect the user to give them visual confirmation
header("location: thanks.php");
exit;
}

YvetteKuhns
10-2-03, 11:51 AM
You say your code isn't working. What error do you get? Or does it ignore your duplication checker? Is your spelling correct? Are your letters all lowercase or matching? There may be a syntax error.

If you copied your code exactly, you need a space:

$con =
mysql_connect("sql02.mysite.com", "myuser", "mypass")or die

should be

$con =
mysql_connect("sql02.mysite.com", "myuser", "mypass") or die

Jeff321
10-2-03, 02:25 PM
Note that the server name should be mysql##.powweb.com (e.g. mysql01.powweb.com), not yoursite.com.

Also, you may want to do the "$chkEmail = ..." line the same way as you did the "$myquery = ..." Store the query in a variable first, then use $result = mysql_query($myquery);

Good luck.

YvetteKuhns
10-2-03, 04:17 PM
Jeff321

Good ideas! I am too tired with the flu to notice something so obvious.