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

时间:2014-02-26 03:07:37

标签: php mysql sql google-maps

我使用Google Maps API实现了一些功能,以便用户可以向地图添加标记。添加标记后,将其存储在“虚拟”表中。

在管理员批准标记之前,应该留在虚拟表中。当管理员批准标记时,应该从虚拟表中删除它,并将其与其余标记一起添加到常规表中。

目前我有一些代码显示虚拟表中的行列表,允许我从表中删除行,但不会将行添加到当前表中。是否有一种简单的方法来修改此代码来执行此操作?

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();
}
?>

2 个答案:

答案 0 :(得分:0)

您可以在删除markersunapproved

中的条目之前添加INSERT
mysql_query("
INSERT INTO [YOUR DESTINATION TABLE] 
SELECT *
FROM markersunapproved
WHERE id = {$idToDelete}
");

答案 1 :(得分:0)

首先,您必须在删除之前复制数据:

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; 

为了使其正常工作,regular_table中的“id”列应该在指定了主索引的情况下自动递增,以便自动生成插入中每行的“id”。

,其次,运行代码中已有的查询:

DELETE FROM dummy_table 
WHERE id = id