deadmasst3r Posted March 3, 2009 Share Posted March 3, 2009 hello everybody, i'm new here and also a biginner in PHP...i've just started learning it from a tutorial found on YT. Here is the code: <?php mysql_connect ("localhost","root") or die(mysql_error()); mysql_select_db("tutorial") or die(mysql_error()); ?> <form method="POST" action="tutorial.php"> <table border="0" style="font-size: 15px; font-family Tahoma; border: 1px solid black;"> <tr> <td> Username: </td> <td> <input type="text" name="username" value=" <?php echo isset($_POST['username']); ?>" </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="submit" value="submit" /> </td> </tr> </table> </form> <?php if(isset($_POST['submit'])) { $username = $_POST['username']; $curnum = 0; if(!$username) { $curnum ++; echo $curnum . ". You didn't enter a username!</br>\n"; } $sql = "SELECT * FROM users WHERE username=' ". $username ." '"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0) { $curnum ++; echo $curnum . ". The username '". $username ."' already exists!</br>\n"; } if($curnum == 0) { mysql_query("INSERT INTO users VALUES(`id`,' ". $username ." ')") or die(mysql_error()); } } ?> When i press submit it says: 1. The username ' ' already exists! , even if i didn't write anything... Can you please help me? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/147791-solved-username-already-exists/ Share on other sites More sharing options...
premiso Posted March 3, 2009 Share Posted March 3, 2009 One You are adding spaces inside the ' of the sql, this is not good practice and will cause different issues. $sql = "SELECT * FROM users WHERE username='". $username ."'"; // and this one here too: mysql_query("INSERT INTO users VALUES(`id`,'". $username ."')") or die(mysql_error()); At least you were consistent. Two you should trim the username coming from post and escape it: $username = mysql_real_escape_string(trim($_POST['username'])); Just incase anyone posts bad characters. Finally you should check if username is populated before moving on: if(empty($username)) { Fix those items and see where that gets you. EDIT: Also your form will show up no matter what, I do not think you want this. I would move the if before the form and put the form in it's own if statement after the if $_POST['submit'] isset. Something like: if ($curnum > 0 || !isset($_POST['submit'])) { Then put the form inside that. So it only shows if there is an error. I would also make the mysql_num_rows part an if else: if(mysql_num_rows($res) > 0) { $curnum ++; echo $curnum . ". The username '". $username ."' already exists!</br>\n"; }else { mysql_query("INSERT INTO users VALUES(`id`,' ". $username ." ')") or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/147791-solved-username-already-exists/#findComment-775775 Share on other sites More sharing options...
deadmasst3r Posted March 4, 2009 Author Share Posted March 4, 2009 first, thanks for your reply...it partially works...when i press submit with no value...it says 1. You didn't enter a username!...but after that...on the box it appears a 1... p.s. i didn't understand where to do this: EDIT: Also your form will show up no matter what, I do not think you want this. I would move the if before the form and put the form in it's own if statement after the if $_POST['submit'] isset. Something like: if ($curnum > 0 || !isset($_POST['submit'])) { Here is again the code modified without that step: <?php mysql_connect ("localhost","root") or die(mysql_error()); mysql_select_db("tutorial") or die(mysql_error()); ?> <form method="POST" action="tutorial.php"> <table border="0" style="font-size: 15px; font-family Tahoma; border: 1px solid black;"> <tr> <td> Username: </td> <td> <input type="text" name="username" value=" <?php echo isset($_POST['username']); ?>" </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="submit" value="submit" /> </td> </tr> </table> </form> <?php if(isset($_POST['submit'])) { $username = mysql_real_escape_string(trim($_POST['username'])); $curnum = 0; if(empty($username)) { $curnum ++; echo $curnum . ". You didn't enter a username!</br>\n"; } $sql = "SELECT * FROM users WHERE username='". $username ."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0) { $curnum ++; echo $curnum . ". The username '". $username ."' already exists!</br>\n"; }else { mysql_query("INSERT INTO users VALUES(`id`,' ". $username ." ')") or die(mysql_error()); } } ?> Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/147791-solved-username-already-exists/#findComment-776269 Share on other sites More sharing options...
kickstart Posted March 4, 2009 Share Posted March 4, 2009 first, thanks for your reply...it partially works...when i press submit with no value...it says 1. You didn't enter a username!...but after that...on the box it appears a 1... You are echoing the "isset" of the field. This will be true or false (ie, 1 or 0) depending on whether it has been set. However as you also have a space in there I think it will always be set, therefore checking isset will always return 1 (so next time round the field will contain a space and a 1). p.s. i didn't understand where to do this: What is probably needed is to move the HTML form from the top of the script down to the bottom (ie, after if has processed the submit), but put it within an if statement (ie, the one given in the reply). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/147791-solved-username-already-exists/#findComment-776281 Share on other sites More sharing options...
deadmasst3r Posted March 5, 2009 Author Share Posted March 5, 2009 can anyone edit my last code and post it here? i didn't manage to solve my problem Sorry for causing this, but I'm still a beginner, that wants to learn more Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/147791-solved-username-already-exists/#findComment-777195 Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 Hi Try this:- <?php mysql_connect ("localhost","root") or die(mysql_error()); mysql_select_db("tutorial") or die(mysql_error()); if(isset($_POST['submit'])) { $username = mysql_real_escape_string(trim($_POST['username'])); $curnum = 0; if(empty($username)) { $curnum ++; echo $curnum . ". You didn't enter a username!</br>\n"; } $sql = "SELECT * FROM users WHERE username='". $username ."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0) { $curnum ++; echo $curnum . ". The username '". $username ."' already exists!</br>\n"; }else { mysql_query("INSERT INTO users VALUES(`id`,' ". $username ." ')") or die(mysql_error()); } } if ($curnum > 0 || !isset($_POST['submit'])) { ?> <form method="POST" action="tutorial.php"> <table border="0" style="font-size: 15px; font-family Tahoma; border: 1px solid black;"> <tr> <td> Username: </td> <td> <input type="text" name="username" value=" <?php echo ((isset($_POST['username'])) ? $_POST['username'] : "") ; ?>" </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="submit" value="submit" /> </td> </tr> </table> </form> <?php } else { echo "Username inserted"; } This should put the form out if there is an error or it is the first time in, otherwise if the username is successfully inserted it will just put up a message to say so. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/147791-solved-username-already-exists/#findComment-777203 Share on other sites More sharing options...
deadmasst3r Posted March 5, 2009 Author Share Posted March 5, 2009 first of all...there is an error : Notice: Undefined variable: curnum in C:\wamp\www\tutorial.php on line 28 After that...if I just press submit without entering anything... I receive this message: 1. You didn't enter a username! 2. The username '' already exists! Quote Link to comment https://forums.phpfreaks.com/topic/147791-solved-username-already-exists/#findComment-777215 Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 Hi For the error, move the initialisation of the $curnum to the start (so it is declared). The other error is because the script checked for a blank username and issued an error if it found one. But then went ahead and tried to insert it anyway. Put an else after the check for a blank username and put the check for an existing username and the insert within that else. Like this <?php mysql_connect ("localhost","root") or die(mysql_error()); mysql_select_db("tutorial") or die(mysql_error()); $curnum = 0; if(isset($_POST['submit'])) { $username = mysql_real_escape_string(trim($_POST['username'])); if(empty($username)) { $curnum ++; echo $curnum . ". You didn't enter a username!</br>\n"; } else { $sql = "SELECT * FROM users WHERE username='". $username ."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0) { $curnum ++; echo $curnum . ". The username '". $username ."' already exists!</br>\n"; }else { mysql_query("INSERT INTO users VALUES(`id`,' ". $username ." ')") or die(mysql_error()); } } } if ($curnum > 0 || !isset($_POST['submit'])) { ?> <form method="POST" action="tutorial.php"> <table border="0" style="font-size: 15px; font-family Tahoma; border: 1px solid black;"> <tr> <td> Username: </td> <td> <input type="text" name="username" value=" <?php echo ((isset($_POST['username'])) ? $_POST['username'] : "") ; ?>" </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="submit" value="submit" /> </td> </tr> </table> </form> <?php } else { echo "Username inserted"; } All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/147791-solved-username-already-exists/#findComment-777222 Share on other sites More sharing options...
deadmasst3r Posted March 5, 2009 Author Share Posted March 5, 2009 thanks man! Quote Link to comment https://forums.phpfreaks.com/topic/147791-solved-username-already-exists/#findComment-777226 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.