2015-04-01 45 views
1

腳本:按鈕從數據庫中刪除記錄

$('.removeVehicle').click(function() { 
    var $this = $(this), 
     $row = $(this).parent().parent(); 

    alert($row.attr('data-vehicle-id')); 

    if (confirm("Delete vehicle? ") == true) { 
     $.post("removevehicle.php", {Id: $row.attr('data-vehicle-id')}); 
    }; 
}); 

HTML/PHP:

<?php while ($row = $products->fetch_assoc()) { ?> 
    <tr data-vehicle-id="<?= $row['Vehicle_ID']?>"> 
     <td class="VRM"><?= $row['VRM']; ?></td> 
     <td class="Make"><?= $row['Make']; ?></td> 
     <td class="Model"><?= $row['Model']; ?></td> 
     <td class="Colour"><?= $row['Colour']; ?></td> 
     <td class="Mileage"><?= $row['Mileage']; ?></td> 
     <td class="Advertised Price">£<?= $row['Advertised_Price']; ?></td> 
     <td class="Date of Registration"><?= $row['Date_of_Registration']; ?></td> 
     <td class="HPi Status"><?= $row['HPI_Status']; ?></td> 
     <td class="actions"> 
      <button class="editLine">Edit line</button> 
      <button class="saveLine hide">Save line</button> 
      <button class="startSale" onclick="div_showSale()">Start Sale</button> 
      <button class="removeVehicle"><img id="trash" src="images/trash.png" alt="Delete Vehicle" height=20 width=20></button> 
     </td> 
    </tr> 
<?php } ?> 

removevehicle PHP:

<?php 
require 'config/init.php'; 

if (isset($_SESSION['myusername'])) { 
    $mysqli = new mysqli($db['hostname'], $db['username'], $db['password'], $db['database']); 

    if ($mysqli->connect_errno) { 
     printf("Connect failed: %s\n", $mysqli->connect_error); 
     exit(); 
    } 
    $queryStr = "DELETE FROM VEHICLE WHERE Vehicle_ID = '" . $_POST['Id'] . "'"; 


    $query = $mysqli->query($queryStr); 

} 

作品最多警報的與車輛點ID(正確的車輛ID被警告)。基本上,我需要做的就是從數據庫中刪除車輛/記錄 - 任何更好的建議或如何獲得當前的方法工作?

一旦我有這個工作,我會改變MySQLi查詢來抵消注入(它還沒有生效)。

+0

您是否嘗試刪除短手AJAX方法以及使用完整的簽名? $,ajax({url:你的php調用,方法:post等... 有時候這對我有用,當$ .Post沒有時 – snowYetis 2015-04-01 19:15:14

+0

PHP代碼是否運行?你說它可以運行在警報狀態它沒有得到郵政編碼? – 2015-04-01 19:27:10

回答

0

我覺得這個問題是與SQL查詢和包裝在引號vehicle_id?

如果需要刪除的行之後,你會想這樣做,在這樣的回調:

$.post("removevehicle.php", {Id: $row.attr('data-vehicle-id')}, function() { 
    // delete the row 
}); 
1

使用.data()獲取您的數據屬性信息。同時返回你的PHP結果並把它轉儲到console。最後,檢查你的控制檯是否有錯誤。使用這個來代替:

PHP:

$query = $mysqli->query($queryStr); 
echo $query; 

JS:

$('.removeVehicle').click(function() { 
    var $this = $(this), 
     $row = $(this).parent().parent(); 

    var vehicle_id = $row.data("vehicle-id"); 

    if (confirm("Delete vehicle? ") == true) { 
     $.post("removevehicle.php", {Id: vehicle_id}, function(result) { 
      console.log(result); 
     }); 
    } 
});