Results 1 to 4 of 4

Thread: Move record to another table before deleting

  1. #1
    Join Date
    Jun 2008

    Move record to another table before deleting

    Hi all. I'm new to php and am trying to get past something that's probably quite simple. I've got a form with a while loop showing a list of tasks. The task can be deleted with this button:

    <a href="delete.php?recordID=<?php echo $row_tasks['id']; ?>">
    <input type="submit" name="submit" id="submit" value="" />

    It posts to delete.php where I've got this code to delete the record.

    PHP Code:
    if ((isset($_GET['recordID'])) && ($_GET['recordID'] != "")) {
    $deleteSQL sprintf("DELETE FROM tasks WHERE id=%s",
    GetSQLValueString($_GET['recordID'], "int"));

    $Result1 mysql_query($deleteSQL$test) or die(mysql_error());

    $deleteGoTo "index.php";
      if (isset(
    $deleteGoTo .= (strpos($deleteGoTo'?')) ? "&" "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
    header(sprintf("Location: %s"$deleteGoTo));

    $query_tasks "SELECT * FROM tasks";
    $Recordset2 mysql_query($query_tasks$test) or die(mysql_error());
    $row_tasks mysql_fetch_assoc($tasks);
    $totalRows_tasks mysql_num_rows($tasks); 
    I need to edit this code so the record being passed inserts into another table before it gets deleted. Can anyone point me in the right direction? Thanks guys

  2. #2
    Join Date
    Aug 2004
    My first suggestion would be to not move it, but instead mark it as deleted. This could either be a "status" field which can have different values to indicate what status applies, including, for example, 'D' for deleted. Or you could just have a separate column only for indicating when it is deleted. This would mean you would update your regular listing queries to include a check of that column's value, e.g.:
    . . . WHERE status != 'D' . . .
    Why I like such an approach is that it makes it easy to generate reports and such since all the historical data is in that one table, plus I just don't like having more tables than I really need.

    If you do want/need to go ahead with the idea of moving it to another table, probably the simplest approach is:
    INSERT INTO new_table (col1, col2, col3)
    SELECT col1, col2, col3 FROM old_table WHERE col1='foo'; -- assuming for this example `col1` is the primary key
    Obviously you want to do that before you then delete that record from `old_table`.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Jun 2008
    Thanks a lot for the tip. I've done what you suggested and updated the record's status instead and it works great

  4. #4
    Join Date
    Oct 2012
    Hi all. I'm novel to php as well as am annoying to get past amazing that's almost certainly quite easy.

    **Links removed by Site Administrator so it doesn't look like you're spamming us. Please don't post them again.**

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center