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
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'].

Link to comment
Share on other sites

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 :) .

Link to comment
Share on other sites

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";
}


 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.