脚本:
$('.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 :(得分: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);
});
}
});
答案 1 :(得分:0)
我认为问题在于sql查询并将vehicle_id包装在引号中?
如果您想要在回调中删除该行,请执行以下操作:
$.post("removevehicle.php", {Id: $row.attr('data-vehicle-id')}, function() {
// delete the row
});