Jump to content

Php not running if statement


Hypersource

Recommended Posts

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.
?>
Link to comment
https://forums.phpfreaks.com/topic/283656-php-not-running-if-statement/
Share on other sites

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

  Quote

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

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.

 

 

  On 11/6/2013 at 3:38 PM, trq said:

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.