plznty Posted May 31, 2009 Share Posted May 31, 2009 Duplicate entry ' ' for key 'username' Comes up when i try and register the same username. How can I make it say "Sorry this username has already been taken" rather than "Duplicate entry ' ' for key 'username'" Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/ Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 Duplicate entry ' ' for key 'username' Comes up when i try and register the same username. How can I make it say "Sorry this username has already been taken" rather than "Duplicate entry ' ' for key 'username'" Before executing the INSERT query in the table, do a check on the existence of the username (SELECT query) supplied through the form and if does not exist, then do the insert, otherwise, show the error message. Does it help? Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846210 Share on other sites More sharing options...
Dathremar Posted May 31, 2009 Share Posted May 31, 2009 Before You do the insert into database do a check if the username already exists, something like: "SELECT table_id FROM users WHERE username = '$your_input_var'" If this statement returns number of rows > 0 then print out "Sorry this username has already been taken" Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846211 Share on other sites More sharing options...
joel24 Posted May 31, 2009 Share Posted May 31, 2009 you can have a custom error message... but it will show up if any sql errors occur, not just username duplicates. $ok = @mysql_query("INSERT rara mysql command") or die("This username has been taken"); otherwise you'd have to use an sql query to see if that username is taken and then use an if command in the php.. i.e. $username = $_POST['username']; $sql = @mysql_query("SELECT username FROM users WHERE username = $username"); if (mysql_num_rows($sql)) { exit("Username has been taken"); } else { $ok = @mysql_query("INSERT rara mysql command"); } you may want to have something like $errorMessage = "Username has been taken" and call that variable in the page rather than using exit... its pretty ugly Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846212 Share on other sites More sharing options...
plznty Posted May 31, 2009 Author Share Posted May 31, 2009 if ( $_GET['view'] == process ) { // Gathered Information $Zuser = $_POST[user]; $pass = $_POST[pass]; $cash = $_POST[cash]; $Zquestion = $_POST[question]; $Zanswer = $_POST[answer]; // Lowercase gathered information $user = strtolower($Zuser); $question = strtolower($Zquestion); $answer = strtolower($Zanswer); mysql_query("INSERT INTO users (id, username, password, money, current, rank, secretquestion, secretanswer) VALUES('', '$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") or die(mysql_error()); echo "User Created!"; } Could you help me more please, i keep getting errors. Thats the registration area. Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846215 Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 if ( $_GET['view'] == process ) { // Gathered Information $Zuser = $_POST[user]; $pass = $_POST[pass]; $cash = $_POST[cash]; $Zquestion = $_POST[question]; $Zanswer = $_POST[answer]; // Lowercase gathered information $user = strtolower($Zuser); $question = strtolower($Zquestion); $answer = strtolower($Zanswer); mysql_query("INSERT INTO users (id, username, password, money, current, rank, secretquestion, secretanswer) VALUES('', '$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") or die(mysql_error()); echo "User Created!"; } Could you help me more please, i keep getting errors. Thats the registration area. If "id" is an auto incremented field, remove '' from the VALUES in the SQL. Your SQL will look like: mysql_query("INSERT INTO users (username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") or die(mysql_error()); Does this help? Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846220 Share on other sites More sharing options...
plznty Posted May 31, 2009 Author Share Posted May 31, 2009 if ( $_GET['view'] == process ) { // Gathered Information $Zuser = $_POST[user]; $pass = $_POST[pass]; $cash = $_POST[cash]; $Zquestion = $_POST[question]; $Zanswer = $_POST[answer]; // Lowercase gathered information $user = strtolower($Zuser); $question = strtolower($Zquestion); $answer = strtolower($Zanswer); mysql_query("INSERT INTO users (id, username, password, money, current, rank, secretquestion, secretanswer) VALUES('', '$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") or die(mysql_error()); echo "User Created!"; } Could you help me more please, i keep getting errors. Thats the registration area. If "id" is an auto incremented field, remove '' from the VALUES in the SQL. Your SQL will look like: mysql_query("INSERT INTO users (username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") or die(mysql_error()); Does this help? It works fine this part how it is. I need help in changing the outcome of duplicate users. Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846221 Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 It works fine this part how it is. I need help in changing the outcome of duplicate users. Try this <?php if ( $_GET['view'] == process ) { // Gathered Information $Zuser = $_POST['user']; $pass = $_POST['pass']; $cash = $_POST['cash']; $Zquestion = $_POST['question']; $Zanswer = $_POST['answer']; // Lowercase gathered information $user = strtolower($Zuser); $question = strtolower($Zquestion); $answer = strtolower($Zanswer); // Check whether $user exists or not $sql = "SELECT `username` FROM `user` WHERE `username` = '$user'"; $rs = mysql_query($sql) or die(mysql_error()); // If number of rows returned is more than 1, it means username is duplicated if ( mysql_num_rows($rs) > 1 ) { echo "Duplicate username"; } else { mysql_query("INSERT INTO users (username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") or die(mysql_error()); echo "User Created!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846227 Share on other sites More sharing options...
plznty Posted May 31, 2009 Author Share Posted May 31, 2009 It works fine this part how it is. I need help in changing the outcome of duplicate users. Try this <?php if ( $_GET['view'] == process ) { // Gathered Information $Zuser = $_POST['user']; $pass = $_POST['pass']; $cash = $_POST['cash']; $Zquestion = $_POST['question']; $Zanswer = $_POST['answer']; // Lowercase gathered information $user = strtolower($Zuser); $question = strtolower($Zquestion); $answer = strtolower($Zanswer); // Check whether $user exists or not $sql = "SELECT `username` FROM `user` WHERE `username` = '$user'"; $rs = mysql_query($sql) or die(mysql_error()); // If number of rows returned is more than 1, it means username is duplicated if ( mysql_num_rows($rs) > 1 ) { echo "Duplicate username"; } else { mysql_query("INSERT INTO users (username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") or die(mysql_error()); echo "User Created!"; } } ?> Still says Duplicate entry '(INPUT TEXT)' for key 'username' Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846230 Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 Try changing if ( $_GET['view'] == process ) to if ( $_GET['view'] == 'process' ) Also, check other scripts linked with this script. Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846233 Share on other sites More sharing options...
PFMaBiSmAd Posted May 31, 2009 Share Posted May 31, 2009 mysql_errno() will return a specific error number (you will have to experiment to find what value it is for the Duplicate entry error) that you can test for and then output your custom error message. Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846235 Share on other sites More sharing options...
plznty Posted May 31, 2009 Author Share Posted May 31, 2009 I had a feeling this would of been straight forward. Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846249 Share on other sites More sharing options...
plznty Posted May 31, 2009 Author Share Posted May 31, 2009 Any more help? Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846282 Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 Any more help? Please provide your HTML / Form part that is posted to the problematic script. Also, the table structure. Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846284 Share on other sites More sharing options...
plznty Posted May 31, 2009 Author Share Posted May 31, 2009 Any more help? Please provide your HTML / Form part that is posted to the problematic script. <?php include ("include/config.php"); echo "<font face='verdana' color='#D2D2D2'> <style type='text/css'> Body { Background: transparent; } </style> <center>"; if ( $_GET['view'] == create ) { echo " <form action='register.php?view=process' method='post'> <table border='0'> <tr> <td colspan='2' align='center'> <b><font face='verdana' color='#D2D2D2'>Registration</font></b> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Username:</font></td> <td><input type=text name=user size=20 maxlength=12></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Password:</font></td> <td><input type=password name=pass size=20 maxlength=20></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>GP (millions)</font></td> <td><input type=text name=cash size=20 maxlength=4></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Secret Question</font></td> <td><input type=text name=question size=20 maxlength=20></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Secret Answer</font></td> <td><input type=text name=answer size=20 maxlength=20></td> </tr> <tr> <td colspan='2' align='center'> <b><input type=submit value=Proceed></b> </tr> </table>"; } if ( $_GET['view'] == process ) { // Gathered Information $Zuser = $_POST['user']; $pass = $_POST['pass']; $cash = $_POST['cash']; $Zquestion = $_POST['question']; $Zanswer = $_POST['answer']; // Lowercase gathered information $user = strtolower($Zuser); $question = strtolower($Zquestion); $answer = strtolower($Zanswer); // Check whether $user exists or not $sql = "SELECT `username` FROM `users` WHERE `username` = '$user'"; $rs = mysql_query($sql) or die(mysql_error()); // If number of rows returned is more than 1, it means username is duplicated if ( mysql_num_rows($rs) > 1 ) { echo "Duplicate username"; } else { mysql_query("INSERT INTO users (username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") or die(mysql_error()); echo "User Created!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846285 Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 Any more help? Please provide your HTML / Form part that is posted to the problematic script. <?php include ("include/config.php"); echo "<font face='verdana' color='#D2D2D2'> <style type='text/css'> Body { Background: transparent; } </style> <center>"; if ( $_GET['view'] == create ) { echo " <form action='register.php?view=process' method='post'> <table border='0'> <tr> <td colspan='2' align='center'> <b><font face='verdana' color='#D2D2D2'>Registration</font></b> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Username:</font></td> <td><input type=text name=user size=20 maxlength=12></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Password:</font></td> <td><input type=password name=pass size=20 maxlength=20></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>GP (millions)</font></td> <td><input type=text name=cash size=20 maxlength=4></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Secret Question</font></td> <td><input type=text name=question size=20 maxlength=20></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Secret Answer</font></td> <td><input type=text name=answer size=20 maxlength=20></td> </tr> <tr> <td colspan='2' align='center'> <b><input type=submit value=Proceed></b> </tr> </table>"; } if ( $_GET['view'] == process ) { // Gathered Information $Zuser = $_POST['user']; $pass = $_POST['pass']; $cash = $_POST['cash']; $Zquestion = $_POST['question']; $Zanswer = $_POST['answer']; // Lowercase gathered information $user = strtolower($Zuser); $question = strtolower($Zquestion); $answer = strtolower($Zanswer); // Check whether $user exists or not $sql = "SELECT `username` FROM `users` WHERE `username` = '$user'"; $rs = mysql_query($sql) or die(mysql_error()); // If number of rows returned is more than 1, it means username is duplicated if ( mysql_num_rows($rs) > 1 ) { echo "Duplicate username"; } else { mysql_query("INSERT INTO users (username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") or die(mysql_error()); echo "User Created!"; } } ?> I have already quoted this. First change this: if ( $_GET['view'] == process ) To: if ( $_GET['view'] == 'process' ) Also: if ( $_GET['view'] == 'create' ) All those lines will produce Notice internally, if the error messages are turned off. Always do this while you develop a script: error_reporting(E_ALL); ini_set('display_errors', '1'); ini_set('log_errors', '0'); in top of your every script. Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846286 Share on other sites More sharing options...
plznty Posted May 31, 2009 Author Share Posted May 31, 2009 Any more help? Please provide your HTML / Form part that is posted to the problematic script. <?php include ("include/config.php"); echo "<font face='verdana' color='#D2D2D2'> <style type='text/css'> Body { Background: transparent; } </style> <center>"; if ( $_GET['view'] == create ) { echo " <form action='register.php?view=process' method='post'> <table border='0'> <tr> <td colspan='2' align='center'> <b><font face='verdana' color='#D2D2D2'>Registration</font></b> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Username:</font></td> <td><input type=text name=user size=20 maxlength=12></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Password:</font></td> <td><input type=password name=pass size=20 maxlength=20></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>GP (millions)</font></td> <td><input type=text name=cash size=20 maxlength=4></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Secret Question</font></td> <td><input type=text name=question size=20 maxlength=20></td> </tr> <tr> <td><font face='verdana' color='#D2D2D2'>Secret Answer</font></td> <td><input type=text name=answer size=20 maxlength=20></td> </tr> <tr> <td colspan='2' align='center'> <b><input type=submit value=Proceed></b> </tr> </table>"; } if ( $_GET['view'] == process ) { // Gathered Information $Zuser = $_POST['user']; $pass = $_POST['pass']; $cash = $_POST['cash']; $Zquestion = $_POST['question']; $Zanswer = $_POST['answer']; // Lowercase gathered information $user = strtolower($Zuser); $question = strtolower($Zquestion); $answer = strtolower($Zanswer); // Check whether $user exists or not $sql = "SELECT `username` FROM `users` WHERE `username` = '$user'"; $rs = mysql_query($sql) or die(mysql_error()); // If number of rows returned is more than 1, it means username is duplicated if ( mysql_num_rows($rs) > 1 ) { echo "Duplicate username"; } else { mysql_query("INSERT INTO users (username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") or die(mysql_error()); echo "User Created!"; } } ?> I have already quoted this. First change this: if ( $_GET['view'] == process ) To: if ( $_GET['view'] == 'process' ) Done.. but it really doesnt help this situation and the problems im having one bit.. ;/ Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846288 Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 Also change: if ( $_GET['view'] == create ) To: if ( $_GET['view'] == 'create' ) All those lines will produce Notice internally and bypasses few lines. Always do this while you develop a script: error_reporting(E_ALL); ini_set('display_errors', '1'); ini_set('log_errors', '0'); in top of your every script. Quote Link to comment https://forums.phpfreaks.com/topic/160358-php-sql-alert/#findComment-846289 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.