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?

Link to comment
Share on other sites

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!

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.