Jump to content

If Statement Not Working Properly


KodiTiger

Recommended Posts

Why is my code below not working properly? I've tried it with == instead of = and it still doesn't work. I've verified using echo statements that $row['name'] = $_POST['name'] and $row['password'] = $_POST['password'] for one of the people in my database, but it still gives the message "Login or password wrong!"

 

Thanks a lot for any help, here is the relevant part of my code (counterInt is defined to be 0 further up):

 

while($row = mysql_fetch_array( $result )) {

 

 

 

if($row['name'] = $_POST['name'] &

 

$row['password'] = $_POST['password']) {

$counterInt++;

session_start();

$_SESSION['name']=$row['name'];

 

}

 

 

}

 

 

 

if(counterInt == 1) {

header( 'Location:

 

http://simbazeviangame.99k.org/game.php' );

}

 

if(counterInt == 0) {

echo "Login or password wrong!";

}

Link to comment
https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/
Share on other sites

'=' is the assignment operator.  '==' is the equality operator.  For if-statements, always use the second one.  Why?  It's always possible to assign a value to a variable.  Further, the statement will actually execute.  In your case, this means you're overwriting $row['password'] with $_POST['password'] and $row['name'] with $_POST['name'].

My latest code (for the whole page this time) is:

 

<?php

 

$counterInt = 0;

 

// Make a MySQL Connection

mysql_connect("localhost", "<username here>", "<password here>") or die(mysql_error());

mysql_select_db("<database name here>") or die(mysql_error());

 

// Get all the data from the "Players" table

$result = mysql_query("SELECT * FROM Players ORDER BY name")

or die(mysql_error()); 

 

 

// Keeps getting the next row until there are no more to get

while($row = mysql_fetch_array( $result )) {

 

 

 

if(($row['name'] == $_POST['name']) && ($row['password'] == $_POST['password'])) {

$counterInt = 1;

session_start();

$_SESSION['name']=$row['name'];

 

}

 

 

}

 

 

 

if(counterInt == 1) {

header( 'Location: http://simbazeviangame.99k.org/game.php' );

}

 

if(counterInt == 0) {

echo "Login or password wrong!";

}

 

?>

 

Thanks a lot :) .

jsut do this instead


$counterInt = 0;

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
$counterInt = 1;
  session_start();
  $_SESSION['name']=$row['name'];
header( 'Location: http://simbazeviangame.99k.org/game.php' );
}
else {
echo "YOU FAIL";
}


 

Thanks for that Rifts, that's great! Now that login.php page is working, my game.php page is as follows:

 

<?php

 

echo "Welcome ";

echo $_SESSION['name'];

echo " , you have logged in successfully.";

 

session_unset();

session_destroy();

 

?>

 

Unfortunately, it doesn't seem to remember the session name from the login page? How can I make it greet me? It currently outputs:

 

"Welcome , you have logged in successfully.

Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in /www/99k.org/s/i/m/simbazeviangame/htdocs/game.php on line 8"

 

Thanks a lot!

oh im sorry its early

 

its not setting the session name correctly you need to throw a while loop in there so

 

if($count==1){
$counterInt = 1;
  session_start();
  $_SESSION['name']=$row['name'];
header( 'Location: http://simbazeviangame.99k.org/game.php' );
}

 

should be

 

if($count==1){
$counterInt = 1;
  session_start();
while($row = mysql_fetch_assoc($result))
{
  $_SESSION['name']=$row['name'];
}
header( 'Location: http://simbazeviangame.99k.org/game.php' );
}

 

 

ok that should work

fyi: the while is unnecessary and potentially bad. also exit() after the header()

 

if($count==1){
     $counterInt = 1;
     $row = mysql_fetch_assoc($result);
     session_start();
     $_SESSION['name']=$row['name'];
     header( 'Location: http://simbazeviangame.99k.org/game.php' );
     exit;
}

I'm not sure :S , the guide I'm using said to do that when I'm done with it. I'm just trying to get a basic login working for now, and so I need the session to be destroyed after the greeting so that I can log in as another member and create a new session. I think, anyway :S ? I'm very new to this...

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.