Jump to content

[SOLVED] Update Query overwriting two columns


mikesta707

Recommended Posts

Ok, so I have run into a weird problem. I have a login system where everytime a user logs in, they have their lastlogin column on my user table updated to now. I recently converted my date columns to type Timestamp. There are a total of two data columns, one is date registered, and one is lastlogin.

 

The problem I am having is when I update the lastlogin column, the other date column gets overwritten also.

Here is the code I got

$dateis = mysql_query("SELECT Now()");
$date = mysql_fetch_assoc($dateis);
$new->query_update('lastlogin', $date['Now()'], 'user', 'User', $User);

 

the query_update function of my class looks like this

function query_update($columns, $values, $table, $where, $whereEqual){//preforms an update query. if columns is an array, value doesnt have to be, but if it is not, all the columns will be updated to that value
	//make sure the required values are set
	$array = array($columns, $values, $table, $where, $whereEqual);
	$this->isempty($array);//function to make sure the values are set
	//verify the arrays, if they are arrays
	if (is_array($columns) && is_array($values)){
		$this->array_verify($columns, $values);//verifies the arrays are the same length
		}
	$sql = "UPDATE ". $table ." SET ";
	if (is_array($columns)){
		$count = count($columns);
		$i = 0;
		foreach($columns as $key => $column){
			if (is_array($values)){
				$set = $values[$key];
				}
			else {
				$set = $values;
				}
			$sql .= $column ."=".$set."";
			$i++;
			if ($i < $count){
				$sql .= ",";
				}
		}
	}
	else {
		$sql = "UPDATE ". $table . " SET ". $columns ."='".$values."'";
		}
	$sql .= " WHERE " . $where ."='".$whereEqual."'";
	$query = mysql_query($sql);
	if (!$query){
		$this->error($this->errors['sql'].' '.mysql_error());
	}
}

if I were to echo the $sql variable it would read what it should, IE

"Update tablename SET lastlogin='Valid Timestamp format date' WHERE User='username'"

 

for my specific date update call. Before, when I had my date columns as varchar (yeah I know, don't yell at me) And called this function, everything worked fine.

 

php version is 5.2.8

mysql version is 5.0.67

 

Anyone have any ideas?

Ha that explains everything. But how should I go about fixing that? I read through the MYSQL page on timestamp, and Im thinking that I should set the default value to null for my date column, and take off the auto update attributes (which i didnt even know it had until i read your post.

 

Thanks!

 

EDIT: Ah excellent yeah I got it. Thanks again!

Archived

This topic is now archived and is closed to further replies.

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