fife Posted August 16, 2010 Share Posted August 16, 2010 Hello im trying to write a simple script so that before my website inserts an account into the database it checks to see if that email already exists. Heres what i got but it does not work. //script for checking if the email account already exists $CheckEmail = "SELECT * FROM Members WHERE email='".$email."'"; $EmailQuery = mysql_query($CheckEmail); $Emailresult = mysql_fetch_array($EmailQuery); $GotEmail = mysql_num_rows($EmailResult); if($GotEmail != 0) { $emailDup= "This email account has already been registered"; } else { //run the rest of my scripts later down the page I have this to echo the message <? if($GotEmail)==1 echo $emailDup; ?> Quote Link to comment https://forums.phpfreaks.com/topic/210856-check-for-duplicates/ Share on other sites More sharing options...
fife Posted August 16, 2010 Author Share Posted August 16, 2010 basically I get this error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sites/yourarena.co.uk/public_html/member_join.php on line 25 which which is this line $GotEmail = mysql_num_rows($EmailResult); Quote Link to comment https://forums.phpfreaks.com/topic/210856-check-for-duplicates/#findComment-1099816 Share on other sites More sharing options...
kenrbnsn Posted August 16, 2010 Share Posted August 16, 2010 You get that error when you have a syntax error in your mysql query. How are you setting the variable $email? Change the line <?php $EmailQuery = mysql_query($CheckEmail); ?> to <?php $EmailQuery = mysql_query($CheckEmail) or die("Problem with the query: $EmailQuery<br>" . mysql_error()); ?> to see the error. Ken Quote Link to comment https://forums.phpfreaks.com/topic/210856-check-for-duplicates/#findComment-1099818 Share on other sites More sharing options...
fife Posted August 16, 2010 Author Share Posted August 16, 2010 i will post my full code now Quote Link to comment https://forums.phpfreaks.com/topic/210856-check-for-duplicates/#findComment-1099824 Share on other sites More sharing options...
fife Posted August 16, 2010 Author Share Posted August 16, 2010 session_start(); $validation_id = strval(time()); if(isset($_POST['submit'])){ //Process data for validation $first_name = trim($_POST['first_name']); $last_name = trim($_POST['last_name']); $DOB = trim($_POST['DOB']); $sex = trim($_POST['sex']); $email = trim($_POST['email']); $username = trim($_POST['username']); $password = trim($_POST['password']); $agree = trim($_POST['agreed']); $creation_date = trim($_POST['creation_date']); $usertype = trim($_POST['usertype']); $access_level = trim($_POST['today_is']); $validation = trim($_POST['to']); $jobs = trim($_POST['jobs']); //Perform validations $errors = array(); if(empty($first_name)) { $errors[] = "Please enter a first name"; } if(empty($last_name)) { $errors[] = "Please enter a surname"; } if(empty($DOB)) { $errors[] = "Please enter your date of birth."; } else if(!(preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $DOB))) { $errors[] = "Please enter your birthday in the format dd/mm/yyyy"; } if(empty($email)) { $errors[] = "Please enter a correct email."; } elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){ $errors[] = "Please enter a valid email [email protected]"; } if(empty($username)) { $errors[] = "Please enter a username."; } if(strlen($password)<6) { $errors[] = "Please enter a password greater than 6 characters long."; } //Check if there were errors if(count($errors)===0) { //Prepare data for db insertion $first_name = mysql_real_escape_string($first_name); $last_name = mysql_real_escape_string($last_name); $DOB = mysql_real_escape_string($DOB); $sex = mysql_real_escape_string($sex); $email = mysql_real_escape_string($email); $username = mysql_real_escape_string($username); $password = md5($password); $agree = mysql_real_escape_string($agree); $creation_date = mysql_real_escape_string($creation_date); $usertype = mysql_real_escape_string($usertype); $access_level = mysql_real_escape_string($access_level); $validation = mysql_real_escape_string($validation); $jobs = mysql_real_escape_string($jobs); //switch date around for database insertion $date_parts = explode('/', $DOB); $DOB_new = "{$date_parts[2]}/{$date_parts[1]}/{$date_parts[0]}"; //script for checking if the email account already exists $CheckEmail = "SELECT * FROM Members WHERE email='".$email."'"; $EmailQuery = mysql_query($CheckEmail); $Emailresult = mysql_fetch_array($EmailQuery); $GotEmail = mysql_num_rows($EmailResult); if($GotEmail != 0) { $emailDup = "This email account has already been registered"; } else { //run the rest of my scripts //insert $query = "INSERT INTO Members (`first_name`, `last_name`, `DOB`, `sex`, `email`, `username`, `password`, `agree`, `creation_date`, `usertype`, `access_level`, `validationID`,`jobs`) VALUES ('{$first_name}', '{$last_name}', '{$DOB_new}', '{$sex}', '{$email}', '{$username}', '{$password}', '{$agree}', '{$creation_date}', '{$usertype}', '{$access_level}', '{$validation}', '{$jobs}')"; $result= mysql_query($query) or die(mysql_error()); //send validation to the thankyou page $url = "thankyou.php?to={$validation}"; header("Location: {$url}"); exit(); } } } then in the middle of the page i have my echo $emailDup Quote Link to comment https://forums.phpfreaks.com/topic/210856-check-for-duplicates/#findComment-1099825 Share on other sites More sharing options...
kenrbnsn Posted August 16, 2010 Share Posted August 16, 2010 Did you change the line I indicated in my previous post? If so, was an error message printed? Ken Quote Link to comment https://forums.phpfreaks.com/topic/210856-check-for-duplicates/#findComment-1099930 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.