Seraskier Posted November 17, 2006 Share Posted November 17, 2006 I need this script to update the database to set online to 0, Logging in change this 0 to 1, now I need it to reverse it. This is my Code:[code]<?phpinclude("connect.php");echo "<form method='post' action='login.php?do=login'>";echo "<table cellpadding='2' cellspacing='0'>";echo "<tr><td>Email Address:</td><td><input type='text' name='email' value='$_email' size='30'></td></tr>";echo "<tr><td>Password:</td><td><input type='password' name='password' value='$_password' size='22'> <input type='submit' value='login'></td></tr>";echo "<tr><td></td><td><div align='right'><a href='register.php'>Register</a> <a href='login.php?do=forgot_pass'>Forgot Password</a></div></td></tr>";echo "</table></form>";if($_GET['do'] == 'login'){$_email = $_POST['email'];$_password = $_POST['password'];$_lastonline = date("M d, Y g:i a");$_SESSION['login'] = 'f384hfdfk894';$_SESSION['email'] = $_email;$result = mysql_query("UPDATE users SET online = '1' WHERE email = '$_email'");$result = mysql_query("UPDATE users SET lastonline = '$_lastonline'");header("location:admin.php");}if($_GET['do'] == 'logout'){$result = mysql_query("UPDATE users SET online = '0' WHERE email = '$_email'");$_SESSION['login'] = '';$_SESSION['email'] = '';echo "<br />You have logged out successfully.<br />";}?>[/code] Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 **bump** Quote Link to comment Share on other sites More sharing options...
tleisher Posted November 17, 2006 Share Posted November 17, 2006 I don't understand your question? Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 OK i have it set to where when a user logs in it will change "online" from 0 to 1 in my database. That works great. Now the problem I have is getting it to where when a user loggs out it will change "online" from 1 to 0 in my database. That better? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 The code you already have looks fine. You might want to check for errors on your queries though:[code=php:0]$sql = "UPDATE users SET online = '1' WHERE email = '$_email'";$result = mysql_query($sql) or die("Error in $sql: " . mysql_error());[/code] Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 [quote author=btherl link=topic=115270.msg469294#msg469294 date=1163724976]The code you already have looks fine. You might want to check for errors on your queries though:[code=php:0]$sql = "UPDATE users SET online = '1' WHERE email = '$_email'";$result = mysql_query($sql) or die("Error in $sql: " . mysql_error());[/code][/quote]No luck. didnt work. Ive tried getting rid of $_SESSION['email'] = ''; but still nothing. Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 **bump** Quote Link to comment Share on other sites More sharing options...
jsladek Posted November 17, 2006 Share Posted November 17, 2006 How does the user select Logout?How does this evaluate true?if($_GET['do'] == 'logout') Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 [quote author=jsladek link=topic=115270.msg469324#msg469324 date=1163728384]How does the user select Logout?How does this evaluate true?if($_GET['do'] == 'logout')[/quote]By clicking a link: [code]if($_GET['action'] == 'logout'){$result = mysql_query("UPDATE users SET online = '0' WHERE email = '$_email'");$_SESSION['login'] = '';$_SESSION['email'] = '';echo "<br />You have logged out successfully.<br />";}[/code]Thats how. Quote Link to comment Share on other sites More sharing options...
jsladek Posted November 17, 2006 Share Posted November 17, 2006 I don't see that anywere in the code you posted Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 Seraskier, can you give more details about what "didn't work". We need to know what behaviour you are expecting and what behaviour you are seeing in order to help you. Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 Ok i'm expecting that when the user loggs out that it will change 1 to 0 and then vice-versa for logging in. But logging in will change it, but logging out wont do nething, itll leave it at 1 and wont change unless i do it manually. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 Ok.. I assume that you see the message "You have logged out successfully", which tells you that the "if" statement worked properly?Then the problem is in your query. When you added the error check for the query, it worked correctly?If so, the next step I would take is to check mysql_affected_rows($result).[code=php:0]print "The query affected " . mysql_affected_rows($result) . " rows<br>";[/code]It should have affected 1 row. If it affected 0 rows, then $_email was probably not set correctly. Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 its telling me [quote]The query affected rowsYou have logged out successfully.[/quote]So I dont know ill show you my code that I put:[code]if($_GET['action'] == 'logout'){$result = mysql_query("UPDATE users SET online = '0' WHERE email = '$_email'");print "The query affected " . mysql_affected_rows($result) . " rows<br>";$_SESSION['login'] = '';$_SESSION['email'] = '';echo "<br />You have logged out successfully.<br />";}[/code]Thats my logout script. I have no clue on what to do. Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 OK great. I got it to work ill show you it for future reference to other people trying to do the same thing.[code]<?phpif($_GET['action'] == 'logout'){$result = mysql_query("UPDATE users SET online = '0'");$_SESSION['login'] = '';echo "<br />You have logged out successfully.<br />";}?>[/code]Thanks guys you guys helped me fix this. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 Seraskier, that will set online = 0 for ALL users. The problem before was that $_email was not set correctly. Try printing out the value of $_email before running your query.Actually since you aren't checking for errors, there's a million things that could go wrong.[b]Check all your mysql_query() calls for errors[/b] Quote Link to comment Share on other sites More sharing options...
jsladek Posted November 17, 2006 Share Posted November 17, 2006 You Just eliminated the where Clause!?!?!? Quote Link to comment Share on other sites More sharing options...
jsladek Posted November 17, 2006 Share Posted November 17, 2006 You got rid of your WHERE Clause!?!?!?! Quote Link to comment Share on other sites More sharing options...
jsladek Posted November 17, 2006 Share Posted November 17, 2006 What happened to the WHERE clause? Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 AHHH i didnt think about that. Ok so now I have the same problem again. Help...Again. Quote Link to comment Share on other sites More sharing options...
jsladek Posted November 17, 2006 Share Posted November 17, 2006 Sorry about all those posts, I'm and Idiot and didn't think I was posting anything... damn second page. I thought the ?!?!?! was messing up the post. Anyhow, as previous poster mentioned. It is probably a problem with the $_email variable not being set Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 Two simple steps will solve your problem.1. Replace your query with [code=php:0]$result = mysql_query("UPDATE users SET online = '0' WHERE email = '$_email'") or die(mysql_error());[/code]2. If you don't get an error, then print the value of $_email. You will probably find it doesn't match. If it does match, then post here again with your current code, as well as the output. Maybe there is some subtle escaping problem with the mysql query. Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 [quote author=btherl link=topic=115270.msg469362#msg469362 date=1163731925]Two simple steps will solve your problem.1. Replace your query with [code=php:0]$result = mysql_query("UPDATE users SET online = '0' WHERE email = '$_email'") or die(mysql_error());[/code]2. If you don't get an error, then print the value of $_email. You will probably find it doesn't match. If it does match, then post here again with your current code, as well as the output. Maybe there is some subtle escaping problem with the mysql query.[/quote]Ok, I dont get any errors and when I try to output $_email nothing shows up at all. But I made it so that it doesnt clear the email session so what else could i try. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 Aha.. I think I see the problem now.Just before the query to set online = '0', add:[code=php:0]$_email = $_SESSION['email'];[/code]That will get the email back out from the session data. Quote Link to comment Share on other sites More sharing options...
Seraskier Posted November 17, 2006 Author Share Posted November 17, 2006 nice that worked. You are so smart. I cannot say thanks enough. Quote Link to comment 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.