Jump to content

Update Database on Logout


Seraskier

Recommended Posts

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]
<?php
include("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'>&nbsp;<input type='submit' value='login'></td></tr>";
echo "<tr><td></td><td><div align='right'><a href='register.php'>Register</a>&nbsp;&nbsp;<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]


Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

its telling me
[quote]
The query affected rows

You 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.
Link to comment
Share on other sites

OK great. I got it to work ill show you it for future reference to other people trying to do the same thing.

[code]
<?php
if($_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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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