deansaddigh Posted June 2, 2010 Share Posted June 2, 2010 I have this code //check again db if user email allready exists $emailcheck =" SELECT email FROM student WHERE email =$email"; $result = mysql_query($emailcheck) or die("error on email check"); $row = mysql_fetch_row($result); And im getting the die error, can anyone tell me why ? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/203619-sql-error/ Share on other sites More sharing options...
deansaddigh Posted June 2, 2010 Author Share Posted June 2, 2010 Ok i was just missing the '' around email . For somereason its still inserting to db even if email exists, can anyone show me what im doing wrong. Here is the code for the check if email exists . //check again db if user email allready exists $emailcheck =" SELECT * FROM student WHERE email ='$email'"; $result = mysql_query($emailcheck) or die("error on email check"); $row = mysql_fetch_row($result); if(!isset($row['email'])) { //Insert enquiry data $query = "INSERT INTO student(student_id, email, first_name, second_name, street, town, country, postcode, password) VALUES(0, '$email', '$firstname', '$surname', '$street', '$town', '$country', '$postcode', '$password')"; $result = mysql_query($query) or die("Error making contact"); } else { echo 'this email address already exists'; } Can anyone see what im doing wrong Quote Link to comment https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066564 Share on other sites More sharing options...
trq Posted June 2, 2010 Share Posted June 2, 2010 You need to check your query returns a record using mysql_num_rows. Quote Link to comment https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066566 Share on other sites More sharing options...
deansaddigh Posted June 2, 2010 Author Share Posted June 2, 2010 So im using the wrong sql function? thank you man Quote Link to comment https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066568 Share on other sites More sharing options...
deansaddigh Posted June 2, 2010 Author Share Posted June 2, 2010 ive looked and changed it to mysql num rows, is it possible to use issett with it like this $row = mysql_num_rows($result); if(!isset($row['email'])) { //Insert enquiry data $query = "INSERT INTO student(student_id, email, first_name, second_name, street, town, country, postcode, password) VALUES(0, '$email', '$firstname', '$surname', '$street', '$town', '$country', '$postcode', '$password')"; $result = mysql_query($query) or die("Error making contact"); } I cant find out if it is, i have tried executing the code and it doesnt seem to work Quote Link to comment https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066573 Share on other sites More sharing options...
deansaddigh Posted June 2, 2010 Author Share Posted June 2, 2010 Thanks for the help got it working with your help. //check again db if user email allready exists $emailcheck =" SELECT * FROM student WHERE email ='$email'"; $result = mysql_query($emailcheck) or die("error on email check"); $row = mysql_num_rows($result); if( mysql_num_rows($result) == 0 ) { //Insert enquiry data $query = "INSERT INTO student(student_id, email, first_name, second_name, street, town, country, postcode, password) VALUES(0, '$email', '$firstname', '$surname', '$street', '$town', '$country', '$postcode', '$password')"; $result = mysql_query($query) or die("Error making contact"); } else { echo 'this email address already exists'; } Only question is why is this if( mysql_num_rows($result) == 0 ) and not if( mysql_num_rows($row) == 0 ) ? I dont understand do i not need this line then $row = mysql_num_rows($result); Quote Link to comment https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066585 Share on other sites More sharing options...
trq Posted June 2, 2010 Share Posted June 2, 2010 mysql_num_rows() expects a result resource and returns a number. If you wanted to use... $row = mysql_num_rows($result); if($row == 0 ) { You could. Quote Link to comment https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066586 Share on other sites More sharing options...
deansaddigh Posted June 2, 2010 Author Share Posted June 2, 2010 Does it matter which way you do it, how would you do it? Thanks by the way Quote Link to comment https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066589 Share on other sites More sharing options...
trq Posted June 2, 2010 Share Posted June 2, 2010 Does it matter which way you do it, how would you do it? Thanks by the way <?php $sql = " SELECT * FROM student WHERE email ='$email'"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { echo 'this email address already exists'; } else { $sql = " INSERT INTO student( student_id, email, first_name, second_name, street, town, country, postcode, password ) VALUES ( 0, '$email', '$firstname', '$surname', '$street', '$town', '$country', '$postcode', '$password' )"; if (!mysql_query($sql)) { trigger_error(mysql_error()); } } } else { trigger_error(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066913 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.