943,791 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 8478
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 1st, 2008
0

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

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dewhickey is offline Offline
7 posts
since Sep 2007
Dec 1st, 2008
0

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

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.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Dec 1st, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dewhickey is offline Offline
7 posts
since Sep 2007
Dec 1st, 2008
0

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

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.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Dec 2nd, 2008
0

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

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):

PHP Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 11
Solved Threads: 12
Junior Poster in Training
sikka_varun is offline Offline
94 posts
since Dec 2008
Dec 2nd, 2008
0

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

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:

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
xarz is offline Offline
24 posts
since Nov 2008
Dec 3rd, 2008
0

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

also u can make a flag that show data is archive if flag is false and non- archive if flag is true
Reputation Points: 15
Solved Threads: 21
Posting Whiz in Training
nikesh.yadav is offline Offline
219 posts
since Feb 2008
Dec 4th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
freeonlinedatin is offline Offline
19 posts
since Nov 2008
Dec 4th, 2008
0

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

To strictly answer the OP's question:

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Feb 10th, 2009
0

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

I am also searching for the same. please let me know if u get the answer.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fedrik is offline Offline
1 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: please help really strange mysql/php error!
Next Thread in PHP Forum Timeline: PHP Error -- HELP !!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC