2014-02-26 109 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 ... –

相關問題