Re: backup of database record before update

From: Date: Sun, 28 Oct 2007 11:58:29 +0000
Subject: Re: backup of database record before update
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-44272@lists.php.net to get a copy of this message
Ronald Wiplinger wrote:
I need some hints how to do that. Maybe there is a template or an easy function available. I have a MySQL database with some tables. Everytime a table record changes, I want also put the old record to a history database. The history table and the original only differs that the key of the original will be just a field in the backup, while the new key is now the UNIX time stamp. How can I do that "easy"?
Just write yourself a little update function, and add a insert call before the actual update. Here is a little update function that I use ($columns and $values get set in a loop before): function mysqlUpdate($table, $columns, $values, $criteria){ $sql = 'UPDATE '.$table.' SET '; for($i = 0; $i < count($columns); $i++){ $sql .= $columns[$i].' = "'.$values[$i].'"'; if($i < count($columns) - 1){
     $sql .= ', ';
} } $sql .= ' WHERE '.$criteria; #debug($sql); return mysql_query($sql); } Since you pass the $criteria (e.g. 'id = "23"') anyways, you can easily call the record first, and copy it to another table. -- Kind regards, hochprior

« previous php.db (#44272) next »