2014-02-26 78 views
0

我有一些功能是通过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(); 
} 
?> 

回答

0

你可以简单地删除条目从markersunapproved前添加一个INSERT

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

奏效谢谢伙计! – Ryan

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 
+0

谢谢@约瑟夫柯林斯 – Ryan

+0

不客气!接受正面反馈jeje ... –

相关问题