Ne.OnZ Posted March 31, 2008 Share Posted March 31, 2008 Hello, I have been in those code dilemma for a while. I have no idea why it does not work. Here is the code: <html> <body> <div align="center"> <?php require("connect.php"); ?> <?php $username = $_POST['user']; $password = sha1($_POST['pass']); $password2 = $_POST['pass2']; $email = $_POST['email']; function error() { if(!$query) { echo "Query Failed: " . mysql_errno() . '<br />' . mysql_error(); } } function check() { mysql_select_db("divnxn5_web"); $result = mysql_query("SELECT `username` FROM `users` WHERE `username` = '$username'"); $error = mysql_query($result) OR die(mysql_error()); while($row = mysql_fetch_array($result)) { echo "The username " . $username . 'is already taken!'; } } if(!$con) { echo "<br /><br />Registering is disabled right now, please check back later."; } else { if(!$_POST['register']) { echo "<br /><br />Use the following form to register a new account with us. <form action='register.php' method='post'><br /><br /> <font size='2'>Username:</font><br /> <input type='text' id='user' name='user' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>Password:</font><br /> <input type='password' id='pass' name='pass' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>Confirm Password:</font><br /> <input type='password' id='pass2' name='pass2' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>E-mail Address:</font><br /> <input type='text' id='email' name='email' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /><br /> <input type='submit' name='register' id='register' value='Register' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' /> <input type='reset' value='Clear' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' /> </form>"; } else { if(!$username) echo "The username you entered encountered a problem.<br /><br />"; check(); if(!$password || !$password2) echo "The password field cannot be left empty!<br /><br />"; if(!$email) echo "The email you entered encountered a problem.<br /><br />"; if(!$password == $password2) echo "The passwords you entered do not match.<br /><br />"; else { mysql_select_db("divnxn5_web"); mysql_query("INSERT INTO users(username, password, email) VALUES('$username', '$password', '$email')"); error(); mysql_close($con); echo "Thank you for registering with us " . $username . '! Enjoy your stay!'; } } } ?> </div> </body> </html> The Error I get is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1 What I'm trying to do is check if the username is taken after they fill it out. Another problem is, it doesn't even check for other problems, the password being empty/not matching or email being empty. I'm using php 4.4.7 if that helps any. Would greatly appreciate it if anyone helps me out. Thank You, Ne.OnZ Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted March 31, 2008 Author Share Posted March 31, 2008 bump Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 1, 2008 Author Share Posted April 1, 2008 bump Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 1, 2008 Share Posted April 1, 2008 To give you a heads up, as a helper I usually ignore people who feel the need to spam themselves to the top less than a day after posting. I'm going to leave you with a hint here Check when you're trying to do with $result. Try echo'ing it. Are you sure it's something you want to query to the database? Quote Link to comment Share on other sites More sharing options...
coder_ Posted April 1, 2008 Share Posted April 1, 2008 One error here: <?php //it should be: $password = sha1($_POST['pass']); $password2 = sha1($_POST['pass2']); if($password != $password2) echo "The passwords you entered do not match.<br /><br />"; ?> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted April 1, 2008 Share Posted April 1, 2008 should be: <?php $result ="SELECT `username` FROM `users` WHERE `username` = '$username'"; $error = mysql_query($result) OR die(mysql_error());?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 1, 2008 Share Posted April 1, 2008 These lines are incorrect: <?php $result = mysql_query("SELECT `username` FROM `users` WHERE `username` = '$username'"); $error = mysql_query($result) OR die(mysql_error()); ?> The should be something like: <?php $query = "SELECT `username` FROM `users` WHERE `username` = '$username'"; $result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error()); ?> Ken Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 1, 2008 Author Share Posted April 1, 2008 In the sticky, it says I can bump after several hours of no response. I tried it the way you guys told me. I tried a username that was already in the database and it did not catch it. It added the same username again. And the other error checking still don't appear. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted April 1, 2008 Share Posted April 1, 2008 <?php $num_rows= mysql_num_rows($username); if($num_rows<=0) { die('no username in database');} elseif($num_rows>1) { die(' you cannot enter more than one username'); } ?> Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 1, 2008 Author Share Posted April 1, 2008 <?php $num_rows= mysql_num_rows($username); if($num_rows<=0) { die('no username in database');} elseif($num_rows>1) { die(' you cannot enter more than one username'); } ?> I get the error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/divnxn5/public_html/register.php on line 23. Followed by No username in database. I checked through phpmyadmin, and there are 2 usernames that I manually entered. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted April 1, 2008 Share Posted April 1, 2008 oops my bad change mysql_num_rows($username) to: mysql_num_rows($result) Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 1, 2008 Author Share Posted April 1, 2008 I still get the same error. Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 2, 2008 Author Share Posted April 2, 2008 bump Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 3, 2008 Author Share Posted April 3, 2008 BumP Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted April 3, 2008 Share Posted April 3, 2008 would u specify the error please ??? Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 3, 2008 Author Share Posted April 3, 2008 Sorry, this is what I get. As you see, same thing as the error mentioned above. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/divnxn5/public_html/register.php on line 23 No Username in database The only mysql_num_rows() I have is the one in the code you gave me. Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 4, 2008 Author Share Posted April 4, 2008 bump Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 4, 2008 Share Posted April 4, 2008 Can you post your code as it now stands? Ken Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 5, 2008 Author Share Posted April 5, 2008 Yes. <?php require("connect.php"); $username = $_POST['user']; $password = sha1($_POST['pass']); $password2 = sha1($_POST['pass2']); $email = $_POST['email']; function error() { if(!$query) { echo "Query Failed: " . mysql_errno() . '<br />' . mysql_error(); } } function exist() { $num_rows = mysql_num_rows($result); if($num_rows<=0) { die('No Username in database'); } elseif($num_rows>1) { die('Cannot enter more then one username!'); } } function check() { mysql_select_db("divnx5_web"); $result = "SELECT `username` FROM `users` WHERE `username` = '$username'"; $error = mysql_query($result) OR die(mysql_error()); while($row = mysql_fetch_array($result)) { echo "The username " . $username . 'is already taken!'; } } if(!$con) { echo "<br /><br />Registering is disabled right now, please check back later."; } else { if(!$_POST['register']) { echo "<br /><br />Use the following form to register a new account with us. <form action='register.php' method='post'><br /><br /> <font size='2'>Username:</font><br /> <input type='text' id='user' name='user' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>Password:</font><br /> <input type='password' id='pass' name='pass' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>Confirm Password:</font><br /> <input type='password' id='pass2' name='pass2' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>E-mail Address:</font><br /> <input type='text' id='email' name='email' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /><br /> <input type='submit' name='register' id='register' value='Register' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' /> <input type='reset' value='Clear' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' /> </form>"; } else { if(!$username) echo "The username you entered encountered a problem.<br /><br />"; exist(); check(); if(!$password || !$password2) echo "The password field cannot be left empty!<br /><br />"; if(!$email) echo "The email you entered encountered a problem.<br /><br />"; if($password != $password2) echo "The passwords you entered do not match.<br /><br />"; else { mysql_select_db("divnx5_web"); mysql_query("INSERT INTO users(username, password, email) VALUES('$username', '$password', '$email')"); error(); mysql_close($con); echo "Thank you for registering with us " . $username . '! Enjoy your stay!'; } } } ?> Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted April 5, 2008 Share Posted April 5, 2008 try this... <?php require("connect.php"); $username = $_POST['user']; $password = sha1($_POST['pass']); $password2 = sha1($_POST['pass2']); $email = $_POST['email']; function error() { if(!$query) { echo "Query Failed: " . mysql_errno() . '<br />' . mysql_error(); } } function exist() { global $result; $num_rows = mysql_num_rows($result); if($num_rows<=0) { die('No Username in database'); } elseif($num_rows>1) { die('Cannot enter more then one username!'); } } function check() { global $result; mysql_select_db("divnx5_web"); $result = "SELECT `username` FROM `users` WHERE `username` = '$username'"; $error = mysql_query($result) OR die(mysql_error()); while($row = mysql_fetch_array($result)) { echo "The username " . $username . 'is already taken!'; } } if(!$con) { echo "<br /><br />Registering is disabled right now, please check back later."; } else { if(!$_POST['register']) { echo "<br /><br />Use the following form to register a new account with us. <form action='register.php' method='post'><br /><br /> <font size='2'>Username:</font><br /> <input type='text' id='user' name='user' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>Password:</font><br /> <input type='password' id='pass' name='pass' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>Confirm Password:</font><br /> <input type='password' id='pass2' name='pass2' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>E-mail Address:</font><br /> <input type='text' id='email' name='email' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /><br /> <input type='submit' name='register' id='register' value='Register' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' /> <input type='reset' value='Clear' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' /> </form>"; } else { if(!$username) echo "The username you entered encountered a problem.<br /><br />"; exist(); check(); if(!$password || !$password2) echo "The password field cannot be left empty!<br /><br />"; if(!$email) echo "The email you entered encountered a problem.<br /><br />"; if($password != $password2) echo "The passwords you entered do not match.<br /><br />"; else { mysql_select_db("divnx5_web"); mysql_query("INSERT INTO users(username, password, email) VALUES('$username', '$password', '$email')"); error(); mysql_close($con); echo "Thank you for registering with us " . $username . '! Enjoy your stay!'; } } } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 5, 2008 Share Posted April 5, 2008 You're having problems with variable scope. You can use the solution benjaminbeazy has proposed or pass the variables into the functions, which is my preferred solution. Something like this: <?php require("connect.php"); $db = mysql_select_db("divnx5_web"); $username = $_POST['user']; $password = sha1($_POST['pass']); $password2 = sha1($_POST['pass2']); $email = $_POST['email']; function error($result, $query) { if(!$result) { echo "Query Failed: $query<br />" . mysql_errno() . '<br />' . mysql_error(); } } function exist($result) { $num_rows = mysql_num_rows($result); if($num_rows<=0) { die('No Username in database'); } elseif($num_rows>1) { die('Cannot enter more then one username!'); } } function check($username) { $query = "SELECT `username` FROM `users` WHERE `username` = '$username'"; $result = mysql_query($query) or die("Problem with the query:$query<br />" . mysql_error()); if (mysql_num_rows($result) > 0) echo "The username " . $username . 'is already taken!'; } if(!$con) { echo "<br /><br />Registering is disabled right now, please check back later."; } else { if(!$_POST['register']) { echo "<br /><br />Use the following form to register a new account with us. <form action='register.php' method='post'><br /><br /> <font size='2'>Username:</font><br /> <input type='text' id='user' name='user' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>Password:</font><br /> <input type='password' id='pass' name='pass' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>Confirm Password:</font><br /> <input type='password' id='pass2' name='pass2' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /> <font size='2'>E-mail Address:</font><br /> <input type='text' id='email' name='email' size='20' style='background-color: #FFFFFF; font-size: 8pt; border: 1 solid #003399' /><br /><br /> <input type='submit' name='register' id='register' value='Register' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' /> <input type='reset' value='Clear' style='background-color: #FFFFFF; color: #000000; font-size: 8pt; border: 1 solid #003399' /> </form>"; } else { if(!$username) echo "The username you entered encountered a problem.<br /><br />"; exist(); check($username); if(!$password || !$password2) echo "The password field cannot be left empty!<br /><br />"; if(!$email) echo "The email you entered encountered a problem.<br /><br />"; if($password != $password2) echo "The passwords you entered do not match.<br /><br />"; else { $query = "INSERT INTO users(username, password, email) VALUES('$username', '$password', '$email')"; $result = mysql_query($query); error($result, $query); mysql_close($con); echo "Thank you for registering with us " . $username . '! Enjoy your stay!'; } } } ?> I tried to fix it as best I could, but your logic doesn't make any sense in many places. Ken Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 5, 2008 Author Share Posted April 5, 2008 Kenrbnsn, your way worked, sort of. It finally says username is taken, but if I leave other fields blank or enter random stuff, then enter a username that isn't taken. It adds the username to the database, even though the passwords or email fields might have problems. I don't see any problems with my logic on that part. ??? Thanks Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted April 5, 2008 Share Posted April 5, 2008 <?php if(!$password || !$password2||!isset($password)||!isset($password2)||empty($password)||empty($password2)) echo "The password field cannot be left empty!<br /><br />"; if(!isset($username)||empty($username)) { echo "The Username was not filled in!<br /><br />"; } ?> Quote Link to comment Share on other sites More sharing options...
Ne.OnZ Posted April 5, 2008 Author Share Posted April 5, 2008 <?php if(!$password || !$password2||!isset($password)||!isset($password2)||empty($password)||empty($password2)) echo "The password field cannot be left empty!<br /><br />"; if(!isset($username)||empty($username)) { echo "The Username was not filled in!<br /><br />"; } ?> That helped out a lot, but it still has some problems. I was messing around with the form so test all conditions. I entered a username that was taken, both passwords correctly and an empty email field. It came back saying, username is taken, email field was empty, but it ended up adding the username to the db again, even though it was taken and the email field was empty. ??? Edit: Also if I only enter a username, and leave all other fields blank, it ends up adding the username to the db with the other fields empty. x.X Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted April 5, 2008 Share Posted April 5, 2008 change your logic man change : <?php } else {//insert }?> to: <?php elseif(isset($username)||isset($password)||isset($password2)||!empty($username)||!empty($password)||!empty($password2) ||!empty($email)||isset($email)) { //insert }?> Quote Link to comment 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.