PHP problem - unknown problem.

+2 Jad Samadi · December 23, 2014
So hey there guys i want to start first by thinking you for helping me in the previous problems i had and well yet i have a new one today .
As i was working on my login/Register system i actually was surprised to realize something new in PHP .... or maybe its just my fault .
Any way let me show you what is happening .
This is my code : 


<?php


include 'core/init.php';





if (empty($_POST) === false) {


$username = $_POST['username'];


$password = $_POST['password'];





          if (empty($username) === true || empty($password) === true) {


             $errors[] = 'You need to enter a username and a password';


          } else if(user_exists($username) === false) {


            $errors[] = 'We can\'t find that username. Have you registered ?';


          }else if (user_active() === false) {


            $errors[] = 'You haven\'t activated your account';


          } else { 


           //here


          }











print_r($errors);


}


?>



i don't suppose any thing is wrong in there yet when you test it you will notice that one of the 'else if' statements don't actually work . ... its like the file is only reading the first one.
Ill explain more . I opened my index page and tried to login with empty username and password boxes . It gives me this error :
Array ( [0] => You need to enter a username and a password )
witch is great ... that means it works !
Now i tried to test if the 'user_exists' function work . So i entered a username and password that isn't saved in my database and i got that error :
Array ( [0] => We can't find that username. Have you registered ? )
Works great ! Now the problem .... When i try to test the 'user_active' function it doesn't work ... I enter the username and password that is saved in my database with the active state equal to 0 where i should then get the error :
You haven\'t activated your account
But instead i get this error :
Array ( [0] => We can't find that username. Have you registered ? )
Now i know i probably made a mistake in the function so it does not work but no i didn't.
I tried to switch the active function and the exists one and my code became :


<?php


include 'core/init.php';





if (empty($_POST) === false) {


$username = $_POST['username'];


$password = $_POST['password'];





          if (empty($username) === true || empty($password) === true) {


             $errors[] = 'You need to enter a username and a password';


          }else if (user_active() === false) {


            $errors[] = 'You haven\'t activated your account';


          } else if(user_exists($username) === false) {


            $errors[] = 'We can\'t find that username. Have you registered ?';


          } else { 


           //here


          }











print_r($errors);


}


?>



And for some reason now i cant get this error :
Array ( [0] => We can't find that username. Have you registered ? )
instead i get this one :
Array ( [0] => You haven't activated your account )
So ye ... for both cases if i enter a correct or incorrect username/password.
I don't think i can explain more and i think this code would help as well(the functions) :


<?php





function user_exists($username){


         $username = sanitize($username);


         return (mysql_result(mysql_query("SELECT COUNT (`user_id`) FROM `users` WHERE `username` = '$username'"), 0) ==1) ? true : false;


}





function user_active($username){


         $username = sanitize($username);


         return (mysql_result(mysql_query("SELECT COUNT (`user_id`) FROM `users` WHERE `username` = '$username' AND `active` = 1"), 0) ==1) ? true : false;


}





?>




So if you can help me i would really appreciate it .Thanks for reading

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Jay Deshaun · December 23, 2014
You forgot to add the '$username' between the brackets of 'user_active'...

Yours:
user_active()


Fix:
user_active($username)

By the way, what's 'sanitize'?
http://php.net/manual-lookup.php?pattern=sanitize&scope=quickref">http://php.net/manual-lookup.php?pattern=sanitize&scope=quickref
0 Jad Samadi · December 23, 2014
i actually did add $username ... but the problem isn't there the main problem is that the text editor or like Apache or what ever is ignoring the second command after the first .... 
0 Jay Deshaun · December 23, 2014
The 
mysql_result


return's strings not numbers.

Try this:

<?php
include 'core/init.php';

if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username) || empty($password))
$errors[] = 'You need to enter a username and a password';
else if(!user_exists($username))
$errors[] = 'We can\'t find that username. Have you registered?';
else if (!user_active($username))
$errors[] = 'You haven\'t activated your account';
else
//here

print_r($errors);
}

function user_exists($username){
$username = sanitize($username);
if($query = mysql_query("SELECT user_id FROM users WHERE username = '$username'"))
if(mysql_result($query,0)=="1")
return true;
return false;
}
function user_active($username){
$username = sanitize($username);
if($query = mysql_query("SELECT user_id FROM users WHERE username = '$username' AND active = 1"))
if(mysql_result($query,0)=="1")
return true;
return false;
}

?>
0 Jad Samadi · December 23, 2014
nope its not working :./ but thanks for trying to fix it :/
0 Ron Butcher · December 24, 2014
The problem on your code is in the COUNT function.  You need to remove the ticks (`) from inside the parenthesis and remove the space between the word COUNT and the parenthesis.

function user_exists($username){
$username = sanitize($username);
return (mysql_result(mysql_query("SELECT COUNT(user_id) FROM `users` WHERE `username` = '$username'"), 0) ==1) ? true : false;
}

function user_active($username){
$username = sanitize($username);
return (mysql_result(mysql_query("SELECT COUNT(user_id) FROM `users` WHERE `username` = '$username' AND `active` = 1"), 0) ==1) ? true : false;
}



The reason it always fails at the first else if statement is because the query fails, and then the function returns false.  It is always a good idea to add some error checking into your mysql queries.  Here is an example:


function user_active($username){
$username = sanitize($username);
$result = mysql_query("SELECT COUNT(user_id) FROM `users` WHERE `username` = '$username' AND `active` = 1") or die(mysql_error());
return (mysql_result($result, 0) ==1) ? true : false;
}

The or die statement will kill the page and print out the error.  Not the perfect solution when you go live with your site, but perfect for troubleshooting in development.


One more thing, mysql is depreciated, start looking into mysqli (MySQL Improved) instead.  It is almost the same as mysql so as you are going through the tutorials it will not take much to modify the code to mysqli.
0 Jad Samadi · December 24, 2014
nope still not working :/ i don't know what is happening ...
0 Jad Samadi · December 24, 2014
I edited the post and added the codes into code format thing .
0 Ron Butcher · December 25, 2014
Did you try the 'or die' statement to see if a mysql error is being produced by your query?
0 Jad Samadi · December 25, 2014
yes i did and the page did die but i don't think the problem is related to the query ....
0 Jay Deshaun · December 26, 2014
I suggest converting to MySQLi like Ron Butcher said.

And 'mysql_result' still returns Strings not Numbers/Ints.
  • 1
  • 2

PHP

107,221 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators