Hypersource Posted November 6, 2013 Share Posted November 6, 2013 (edited) So I have a basic login script wich checks if the users name was already in the database and if not, puts the data in the database. The problem is that its not checking if the user is already in the database? I have the code in there aswell though? <?php include 'template.php'; // includes the basic connect thing $username = $_GET['username']; if($_GET['postback'] != '1') { echo' <h2>Signup!</h2> <form action=signup.php> <br> <input name="username" placeholder=Username><br> <input name="password" placeholder=Password><br> <input name="email" placeholder=Email type=email><br> <input type="hidden" name="postback" value ="1"> <input type="submit" value ="Sign up!"> </form> '; } else { echo "<br>Signing up...<br>"; $sql="SELECT * FROM `users`"; echo $sql; $result = mysqli_query($con,$sql) or die("ERROR"); $i = 0; echo $i; while($row = mysqli_fetch_array($result)) { if($row['username'] == $username) { $i = 1; echo "That username is in use! Please pick another one."; } } if ($i != 1) { $error = 0; if (strlen($_GET['password']) < 6) { echo "Your password has to be at least 6 characters long!"; $error = 1; } if (strlen($_GET['username']) < 3) { $error = 1; echo "Your username has to be at least 3 characters long!"; } if ($error == 1){ echo' <h2>Signup!</h2> <form action=signup.php> <br> <input name=username placeholder=Username><br> <input name=password placeholder=Password><br> <input name=email placeholder=Email type=email><br> <input type="hidden" name="postback" value ="1"> <input type="submit" value ="Sign up!"> </form> '; } else if($i==0 and $error == 0){ $sql = "INSERT INTO `users`(username,password,email) VALUES('".$_GET['username']."','".$_GET['password']."','".$_GET['email']."')"; print $sql; //mysqli_query($con,$sql); } } else { echo "<br>That username is already taken.<br>"; } } ?> <?php include 'templatend.php'; // closes divs etc form the template file. ?> Edited November 6, 2013 by Hypersource Quote Link to comment https://forums.phpfreaks.com/topic/283656-php-not-running-if-statement/ Share on other sites More sharing options...
Solution AdRock Posted November 6, 2013 Solution Share Posted November 6, 2013 (edited) Instead of querying the database for all the users, why don't you do a SELECT COUNT from `users` WHERE username=$username Do a numrows and if it's 1 then it's taken else it's free. Just noticed in your while loop were you set $i = 1, you should exit the loop otherwise it will overwrite it Edited November 6, 2013 by AdRock Quote Link to comment https://forums.phpfreaks.com/topic/283656-php-not-running-if-statement/#findComment-1457217 Share on other sites More sharing options...
trq Posted November 6, 2013 Share Posted November 6, 2013 Instead of querying the database for all the users, why don't you do a SELECT COUNT from `users` WHERE username=$username Do a numrows and if it's 1 then it's taken else it's free. The number of rows returned from a "SELECT COUNT" query will always be 1. You would actually need to check to see that the count returned is 1. Or better still, just do a SELECT query and check if a row was returned. Quote Link to comment https://forums.phpfreaks.com/topic/283656-php-not-running-if-statement/#findComment-1457218 Share on other sites More sharing options...
objnoob Posted November 6, 2013 Share Posted November 6, 2013 (edited) Instead of querying the database for all the users, why don't you do a SELECT COUNT from `users` WHERE username=$username Do a numrows and if it's 1 then it's taken else it's free. Queries that utilize aggregate functions will always return at least 1 row. SELECT COUNT(null); returns 1 row with the return value from count function ( and I didn't even specify a table ) SELECT MIN(null); returns 1 row with the return value from the min function. SELECT AVG(null); returns 1 row with the return value from the avg function. Even if the table(s) your querying are completely empty! Ultimately, It's your responsibility to fetch and compare the value. The number of rows returned from a "SELECT COUNT" query will always be 1. You would actually need to check to see that the count returned is 1. Or better still, just do a SELECT query and check if a row was returned. Excellent! Edited November 6, 2013 by objnoob Quote Link to comment https://forums.phpfreaks.com/topic/283656-php-not-running-if-statement/#findComment-1457219 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.