Reset mysql field back to default

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Reset mysql field back to default

Post by Jim_Bo »

Hi,

Apon logout I am tyring to get the script to reset a mysql field back to the default value, so when people logout there username is droped from the logged in members list:

Code: Select all

if(!isset($_REQUEST['logmeout'])){
	echo "<center><font class=\"txt\">Are you sure you want to logout?</font></center><br>";
	echo "<center><font class=\"txt\"><a href=../index.php?pages=logout&logmeout>Yes</a> | <a href=javascript:history.back()>No</a></font><br>";
} else {
	
	

	$sql = "DELETE FROM ppl_online WHERE session_id = '".session_id()."'";
	$sql = "UPDATE users SET online_status = '0000-00-00 00:00:00' WHERE user_name='".$_SESSION['user_name']."'";
	$result = mysql_query($sql) or die("Database failure");
	
	session_destroy();
	header("Location: ../login/logedout.php");
}
The following peice of code doesnt seem to be working:

Code: Select all

$sql = "UPDATE users SET online_status = '0000-00-00 00:00:00' WHERE user_name='".$_SESSION['user_name']."'";
?

Thanks
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

a better method would be to alter the field to hold a DEFAULT value. This means that if you issue an INSERT command or UPDATE to a field with nothing in it, it would resort to what you set as default.

example :

say we create a field in a table

Code: Select all

create table bob (myfield varchar(30) DEFAULT 'default value we want' NOT NULL)
we can then issue a blank value for the field, and the default will be registerd..

Code: Select all

mysql_query("insert into mytable (myfield) VALUES ('')");
hope this helps.
Last edited by infolock on Thu Mar 31, 2005 12:45 am, edited 3 times in total.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

That makes sence .. but how do I make a blank insert rather than inserting online_status = '0000-00-00 00:00:00':

Code: Select all

$sql = "UPDATE users SET online_status = '0000-00-00 00:00:00' WHERE user_name='".$_SESSION['user_name']."'";
?

Thanks
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Code: Select all

$sql = "UPDATE users SET online_status = '' WHERE user_name='".$_SESSION['user_name']."'";
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Yea I tried that .. But it seems to keep holding a time value .. the time gets inserted everytime there is a page change .. But once the logout happens they are sent to a page that doesnt includes session_start() .. so when the logout script runs the field should be reset to default value .. It isnt updated untill the login process happens in which that field is then updated using now() .. So I pretty sure that on logout, the field is not being reset back to the default value .. with any of the above posted codes ..

Thanks
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

then before you destroy the ession, update the field.


also, did ou alter the field type to include a default value?..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not just delete the user from the online_status?
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

I allready have a default and also the query is run before the session is destroyed .. as you will see in the original post ..

.. the online_status is being read from a field within the users table .. Can you just delete a single field? .. I cant delete the entire record as all the users data is held in it.

Maybe I should be using a seperate table?

Thanks
Last edited by Jim_Bo on Thu Mar 31, 2005 1:22 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Considering you are updating that particular field all the time, your database may perform better if it was in a seperate table. One designed for higher activity (less fields, simple types and stuff). I don't know about other database table storage types, but mysql has 'heap' among other types.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

On forums like this one etc .. how is the guest online, members online etc handled .. would it all be held in a single table ..

I have guest/members & total visits in one table .. and basically using the datetime method from the users table to get the members who are active online to display the user_name on the webpage ..?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

phpbb uses a single table for all users online, guest and members alike. User data is only for members, so that data is stored in its own table. There's a user for guests that all guests share though, mostly to store the basic starting information for a user on the system.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Jim_Bo wrote:Hi,

I allready have a default and also the query is run before the session is destroyed .. as you will see in the original post ..

.. the online_status is being read from a field within the users table .. Can you just delete a single field? .. I cant delete the entire record as all the users data is held in it.

Maybe I should be using a seperate table?

Thanks
if you already have a default and set it up like i showed you, there is no reason why that when you send a blank value for that field that it doesn't fill it with the default value... :?
Post Reply