简体   繁体   English

从一个表删除行并添加到另一个表

[英]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. 我使用Google Maps API实现了一些功能,以便用户可以向地图添加标记。 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 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 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 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 您可以在从markersunapproved删除条目之前简单地添加一个INSERT

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. 为了使其正常工作,regular_table中的“ id”列应自动递增并分配主索引,以便自动生成插入中每一行的“ id”。

and second, run the query that is already in your code: 其次,运行代码中已经存在的查询:

DELETE FROM dummy_table 
WHERE id = id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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