KodiTiger Posted December 22, 2010 Share Posted December 22, 2010 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!"; } Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/ Share on other sites More sharing options...
trq Posted December 22, 2010 Share Posted December 22, 2010 == is the comparison operator not = Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150300 Share on other sites More sharing options...
KevinM1 Posted December 22, 2010 Share Posted December 22, 2010 '=' 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']. Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150301 Share on other sites More sharing options...
eddyys Posted December 22, 2010 Share Posted December 22, 2010 Also, && is and, not just & if($row['name'] == $_POST['name'] && $row['password'] == $_POST['password']) { Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150302 Share on other sites More sharing options...
KodiTiger Posted December 22, 2010 Author Share Posted December 22, 2010 Okay, I've changed the =s to ==s and & to && in the if statement. It still comes up with username or password wrong... So something else is wrong :S ... Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150305 Share on other sites More sharing options...
shlumph Posted December 22, 2010 Share Posted December 22, 2010 Post your latest code Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150316 Share on other sites More sharing options...
KodiTiger Posted December 22, 2010 Author Share Posted December 22, 2010 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 . Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150339 Share on other sites More sharing options...
Rifts Posted December 22, 2010 Share Posted December 22, 2010 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150348 Share on other sites More sharing options...
KodiTiger Posted December 22, 2010 Author Share Posted December 22, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150355 Share on other sites More sharing options...
Rifts Posted December 22, 2010 Share Posted December 22, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150356 Share on other sites More sharing options...
BlueSkyIS Posted December 22, 2010 Share Posted December 22, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150357 Share on other sites More sharing options...
Rifts Posted December 22, 2010 Share Posted December 22, 2010 I did not know this can you explain more? I'm curious Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150359 Share on other sites More sharing options...
KodiTiger Posted December 22, 2010 Author Share Posted December 22, 2010 I've just tried both of those things and I still get the error and output given in reply #8 . Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150360 Share on other sites More sharing options...
Rifts Posted December 22, 2010 Share Posted December 22, 2010 why are you destroying your session Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150361 Share on other sites More sharing options...
KodiTiger Posted December 22, 2010 Author Share Posted December 22, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150362 Share on other sites More sharing options...
KodiTiger Posted December 22, 2010 Author Share Posted December 22, 2010 Okay, I got rid of the session destroy thing, it's still not putting the username where it should ... Is just blank. Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150363 Share on other sites More sharing options...
Rifts Posted December 22, 2010 Share Posted December 22, 2010 OK well you need to make sure any page you are using a session has session_start(); at the top of it. so put that at the top of both pages Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150364 Share on other sites More sharing options...
KodiTiger Posted December 22, 2010 Author Share Posted December 22, 2010 Yay, success, thanks so much for all of your help everyone ! Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150366 Share on other sites More sharing options...
Rifts Posted December 22, 2010 Share Posted December 22, 2010 sure if you have any other questions feel free to ask Quote Link to comment https://forums.phpfreaks.com/topic/222384-if-statement-not-working-properly/#findComment-1150369 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.