简体   繁体   中英

Deleting rows from one table and adding to another

I have some functionality implemented with Google maps API so that a user can add a marker to the map. When the marker is added, it is then stored in 'dummy' table.

It is supposed to stay in the dummy table until an administrator approves the marker. When the administrator approves the marker it should then be deleted from the dummy table and added to the regular table with the rest of the markers.

Currently I have some code below that displays a list of rows from the dummy table and allows me to delete the rows from the table but it does not add the rows to the current table. Is there a simple way to modify this code to do this?

index.php - Jquery

$(document).ready(function() {

    //##### Send delete Ajax request to response.php #########
    $("body").on("click", "#responds .del_button", function(e) {
         e.returnValue = false;
         var clickedID = this.id.split('-'); //Split string (Split works as PHP explode)
         var DbNumberID = clickedID[1]; //and get number from array
         var myData = 'recordToDelete='+ DbNumberID; //build a post data structure

            jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "response.php", //Where to make Ajax calls
            dataType:"text", // Data type, HTML, json etc.
            data:myData, //Form variables
            success:function(response){
                //on success, hide  element user wants to delete.
                $('#item_'+DbNumberID).fadeOut("slow");
            },
            error:function (xhr, ajaxOptions, thrownError){
                //On error, we alert user
                alert(thrownError);
            }
            });
    });

});

Index.php - PHP

<?php
//include db configuration file
include_once("config.php");

//MySQL query
$Result = mysql_query("SELECT * FROM markersunapproved");

//get all records from markersunapproved table
while($row = mysql_fetch_array($Result))
{
  echo '<li id="item_'.$row["id"].'">';
  echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id"].'">';
  echo '<img src="images/icon_del.gif" border="0" />';
  echo '</a></div>';
  echo $row["name"];
  echo $row["address"];
  echo $row["lat"];
  echo $row["lng"];
  echo $row["type"].'</li>';

}


//close db connection
mysql_close($connecDB);
?>

response.php

<?php
//include db configuration file
include_once("config.php");


if(isset($_POST["recordToDelete"]) && strlen($_POST["recordToDelete"])>0 && is_numeric($_POST["recordToDelete"]))
{   //do we have a delete request? $_POST["recordToDelete"]

    //sanitize post value, PHP filter FILTER_SANITIZE_NUMBER_INT removes all characters except digits, plus and minus sign.
    $idToDelete = filter_var($_POST["recordToDelete"],FILTER_SANITIZE_NUMBER_INT); 

    //try deleting record using the record ID we received from POST
    if(!mysql_query("DELETE FROM markersunapproved WHERE id=".$idToDelete ) )
    {    

    //If mysql delete query was unsuccessful, output error 
        header('HTTP/1.1 500 Could not delete record!');
        exit();

    }
    mysql_close($connecDB); //close db connection
}
else
{
    //Output error
    header('HTTP/1.1 500 Error occurred, Could not process request!');
    exit();
}
?>

You could simply add an INSERT before deleting the entry from markersunapproved

mysql_query("
INSERT INTO [YOUR DESTINATION TABLE] 
SELECT *
FROM markersunapproved
WHERE id = {$idToDelete}
");

First you must copy the data before deleting:

INSERT INTO regular_table 
            (name, 
             address, 
             lat, 
             lng, 
             type) 
SELECT name, 
       address, 
       lat, 
       lng, 
       type 
FROM   dummy_table 
WHERE  1 
       AND dummy_table.id = id; 

For this to work correctly the "id" column in the regular_table should be auto-increment with primary index assigned, in order to autogenerate the "id" of each row in the insert.

and second, run the query that is already in your code:

DELETE FROM dummy_table 
WHERE id = id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM