Move a record from one table to another table in a database using php

Reply

Join Date: Sep 2007
Posts: 7
Reputation: dewhickey is an unknown quantity at this point 
Solved Threads: 0
dewhickey dewhickey is offline Offline
Newbie Poster

Move a record from one table to another table in a database using php

 
0
  #1
Dec 1st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Move a record from one table to another table in a database using php

 
0
  #2
Dec 1st, 2008
why don't you just use a field in the table telling whether or not its archived. just add an archive column with a 0 or 1. where 0 means not archived and 1 means it is.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 7
Reputation: dewhickey is an unknown quantity at this point 
Solved Threads: 0
dewhickey dewhickey is offline Offline
Newbie Poster

Re: Move a record from one table to another table in a database using php

 
0
  #3
Dec 1st, 2008
It sounds like you're talking about testing to see if it's archived. This will always come back a false because I don't know how to archive the record in a table that is a different table from the one it is currently in, which is the record I want to delete.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Move a record from one table to another table in a database using php

 
0
  #4
Dec 1st, 2008
i don't think i understand what you are trying to do correctly. please give a more detailed explaination.
Last edited by kkeith29; Dec 1st, 2008 at 8:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

Re: Move a record from one table to another table in a database using php

 
0
  #5
Dec 2nd, 2008
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):

  1. <?php
  2.  
  3. $id=$_GET['id'];
  4.  
  5. $sql="Select * from <tablename> where id=".$id;
  6. $result=mysql_query($sql);
  7. $row=mysql_fetch_array($result);
  8.  
  9. //Call the function to archive the table
  10. //Function definition is given below
  11. archive_record(<tablename>,$row);
  12.  
  13. //Once you archive, delete the record from original table
  14.  
  15. $sql = "Delete from <tablename> where id=".$id;
  16. mysql_query($sql);
  17.  
  18.  
  19. function archive_record($archived_tablename,$row)
  20. {
  21. $sql = "insert into $archived_tablename values(";
  22. $i=0;
  23. while($i<(count($row)-1))
  24. {
  25. $sql.="'".$row[$i]."',";
  26. }
  27. $i=$i+1;
  28.  
  29. $sql.="'".$row[$i]."'";
  30. $sql.=")";
  31.  
  32. mysql_query($sql);
  33. return true;
  34. }
  35. ?>
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: xarz is an unknown quantity at this point 
Solved Threads: 1
xarz's Avatar
xarz xarz is offline Offline
Newbie Poster

Re: Move a record from one table to another table in a database using php

 
0
  #6
Dec 2nd, 2008
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:

  1. $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 ::
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 213
Reputation: nikesh.yadav is an unknown quantity at this point 
Solved Threads: 17
nikesh.yadav's Avatar
nikesh.yadav nikesh.yadav is offline Offline
Posting Whiz in Training

Re: Move a record from one table to another table in a database using php

 
0
  #7
Dec 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 19
Reputation: freeonlinedatin is an unknown quantity at this point 
Solved Threads: 0
freeonlinedatin's Avatar
freeonlinedatin freeonlinedatin is offline Offline
Newbie Poster

Re: Move a record from one table to another table in a database using php

 
0
  #8
Dec 4th, 2008
hi

This is harrison i agree with xarz,you just create a separate column ie for archive its some 0 and unarchive its 1,let's try and i think its work for you
Last edited by freeonlinedatin; Dec 4th, 2008 at 6:54 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: Move a record from one table to another table in a database using php

 
0
  #9
Dec 4th, 2008
To strictly answer the OP's question:

  1. LOCK TABLES active WRITE, archive WRITE;
  2. INSERT INTO archive
  3. SELECT * FROM active
  4. WHERE active_rec_id='$confirmed_id';
  5. DELETE FROM active
  6. WHERE active_rec_id='$confirmed_id';
  7. 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1
Reputation: fedrik is an unknown quantity at this point 
Solved Threads: 0
fedrik fedrik is offline Offline
Newbie Poster

Re: Move a record from one table to another table in a database using php

 
0
  #10
Feb 10th, 2009
I am also searching for the same. please let me know if u get the answer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC