prezident Posted December 8, 2010 Share Posted December 8, 2010 I'm making a registration form and i want to add a user when i click the registration button, but the user can not already be in the database, i set the user to be unique inside of the database and when i try to add a new user it gives an error message, but instead of that error message i would like to input please enter another user-name or user already exist something like that the error message says duplicate entry 'username' inside of db basically... can someone help me with this ? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/ Share on other sites More sharing options...
trq Posted December 8, 2010 Share Posted December 8, 2010 Don't use die() to trap errors. Its the worst thing you can possibly do. Handle the error yourself. if (mysql_query($sql)) { // success } else { // failure } Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144303 Share on other sites More sharing options...
prezident Posted December 8, 2010 Author Share Posted December 8, 2010 Don't use die() to trap errors. Its the worst thing you can possibly do. Handle the error yourself. if (mysql_query($sql)) { // success } else { // failure } ok thank you for the advice ... Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144304 Share on other sites More sharing options...
PFMaBiSmAd Posted December 8, 2010 Share Posted December 8, 2010 And in your // failure logic you can test the value that mysql_errno returns to see if the query failed due to a duplicate key (the value returned should be 1062, but you should test this to make sure) and setup your message and logic needed to prompt the user for a different username. Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144305 Share on other sites More sharing options...
prezident Posted December 8, 2010 Author Share Posted December 8, 2010 And in your // failure logic you can test the value that mysql_errno returns to see if the query failed due to a duplicate key (the value returned should be 1062, but you should test this to make sure) and setup your message and logic needed to prompt the user for a different username. thank you after i made the changes above - to handle the errors myself instead of using die its being weird instead of catching the duplicated user-name its the user go through but not saving it to the database. Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144308 Share on other sites More sharing options...
trq Posted December 8, 2010 Share Posted December 8, 2010 Post your code. Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144313 Share on other sites More sharing options...
prezident Posted December 8, 2010 Author Share Posted December 8, 2010 instead of using the unique key i just tried to use the while loop to see if it will work that way test but here is the code : [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144317 Share on other sites More sharing options...
trq Posted December 8, 2010 Share Posted December 8, 2010 The comparison operator is == not = There's no need to check if $user equals $username though, you know if your query returns any results that the two must match because you used a WHERE clause in your query. ps: Can you please post your code in the forum itself. Posting images will make it difficult for people to copy your code in order to help you. Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144320 Share on other sites More sharing options...
prezident Posted December 8, 2010 Author Share Posted December 8, 2010 The comparison operator is == not = There's no need to check if $user equals $username though, you know if your query returns any results that the two must match because you used a WHERE clause in your query. ps: Can you please post your code in the forum itself. Posting images will make it difficult for people to copy your code in order to help you. ok i'm going to edit it right now and resend im sorry i didn't know how to do it i guess i just copy and paste the code huh ? Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144321 Share on other sites More sharing options...
prezident Posted December 8, 2010 Author Share Posted December 8, 2010 $result = mysql_query("SELECT * FROM login WHERE user ='$user'"); $row = mysqli_num_rows($result); //$sql = mysql_fetch_array($row); //$usernme = $row['user']; if ($row > 0 ) { echo "USERNAME ALREADY VALID PLEASE ENTER A NEW USER"; } else { mysql_query("INSERT INTO login(fname, lname, user, pass, dob) VALUES ('$fname', '$lname', '$user', '$pass','$DOB')"); echo "<b> YOUR ACCOUNT HAS BEEN ADDED <br/> "; } ?> this is the updated code it's still not working I'm thinking the problem is with the if ($row > 0) part.... i think i should use ($row == 0) but if i do that all new users get echo "USERNAME ALREADY VALID PLEASE ENTER A NEW USER <br/>"; Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144323 Share on other sites More sharing options...
trq Posted December 8, 2010 Share Posted December 8, 2010 If $row is bigger than 0 then the user does already exist. There is nothing wrong with your code. Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144342 Share on other sites More sharing options...
prezident Posted December 8, 2010 Author Share Posted December 8, 2010 If $row is bigger than 0 then the user does already exist. There is nothing wrong with your code. ok thanx its still giving me the wrong echo but i will keep debugging to come up with the problem... Quote Link to comment https://forums.phpfreaks.com/topic/220993-registration-form-help/#findComment-1144357 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.