| | |
Move a record from one table to another table in a database using php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2007
Posts: 7
Reputation:
Solved Threads: 0
Using PHP, I have a button to delete the current record from the database. This takes the user to a confirm delete page. When the user confirms the delete, I want the record to be deleted from the table and also moved to another table that functions as an archive table.
Does anyone have the PHP code to do this?
Thanks
Does anyone have the PHP code to do this?
Thanks
Hii...
Use the following function to copy the data from one table to the archived table..
When someone deletes the current record, you must be having its ID... So you can use its ID in the query string..
So once someone deletes this record.. you can write the following code on the page (after the confirm delete page):
Use the following function to copy the data from one table to the archived table..
When someone deletes the current record, you must be having its ID... So you can use its ID in the query string..
So once someone deletes this record.. you can write the following code on the page (after the confirm delete page):
PHP Syntax (Toggle Plain Text)
<?php $id=$_GET['id']; $sql="Select * from <tablename> where id=".$id; $result=mysql_query($sql); $row=mysql_fetch_array($result); //Call the function to archive the table //Function definition is given below archive_record(<tablename>,$row); //Once you archive, delete the record from original table $sql = "Delete from <tablename> where id=".$id; mysql_query($sql); function archive_record($archived_tablename,$row) { $sql = "insert into $archived_tablename values("; $i=0; while($i<(count($row)-1)) { $sql.="'".$row[$i]."',"; } $i=$i+1; $sql.="'".$row[$i]."'"; $sql.=")"; mysql_query($sql); return true; } ?>
I think I agree with kkeith29..
This might not answer your question directly on how to move a certain data from one table to another but instead, you can add a new column in the database which indicate whether it is archived or not. Well, let's assume that archived data are indicated by 1.
if you're using mySQL and php perhaps you can get the list of the archived files by doing this:
just replace the _table_ with your current table name.
This might not answer your question directly on how to move a certain data from one table to another but instead, you can add a new column in the database which indicate whether it is archived or not. Well, let's assume that archived data are indicated by 1.
if you're using mySQL and php perhaps you can get the list of the archived files by doing this:
php Syntax (Toggle Plain Text)
$query=mysql_query("SELECT * FROM _table_ WHERE archive='1'");
just replace the _table_ with your current table name.
Last edited by xarz; Dec 2nd, 2008 at 10:43 pm.
:: xarz ::
also u can make a flag that show data is archive if flag is false and non- archive if flag is true
Help as an alias
I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
To strictly answer the OP's question:
The LOCK and UNLOCK are shown in case you want/need 'atomic' updates.
This worked for a simple table. It might not for a complex table. I believe the two tables must have identical schemas; if not, making them identical makes the programmers life a little easier.
PHP Syntax (Toggle Plain Text)
LOCK TABLES active WRITE, archive WRITE; INSERT INTO archive SELECT * FROM active WHERE active_rec_id='$confirmed_id'; DELETE FROM active WHERE active_rec_id='$confirmed_id'; UNLOCK TABLES;
The LOCK and UNLOCK are shown in case you want/need 'atomic' updates.
This worked for a simple table. It might not for a complex table. I believe the two tables must have identical schemas; if not, making them identical makes the programmers life a little easier.
Last edited by Fest3er; Dec 4th, 2008 at 6:18 pm. Reason: Added another thought or two.
![]() |
Similar Threads
- Loops (PHP)
- Creating an event or an action to a button. (PHP)
Other Threads in the PHP Forum
- Previous Thread: please help really strange mysql/php error!
- Next Thread: Simple Forum Script?
| Thread Tools | Search this Thread |
ajax apache api array basics beginner binary bounce broken cakephp checkbox class cms code codingproblem combobox cron curl database date display dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration interactive ip java javascript joomla js limit link login mail menu mlm mobile multiple mysql nodes oop outofmemmory paging parse paypal pdf php problem procedure query radio ram random recursion regex remote return script search server sessions smash sms soap source space sql syntax system table tutorial up-to-date update upload url validation validator variable video web webapplications websitecontactform xml youtube






