BrettBlade
BrettBlade

Reputation: 82

Button to delete record from database

Script:

$('.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);

}

Works up to the point of the alert with the vehicle ID (correct vehicle ID is alerted). Essentially all I need to do is delete the vehicle/record from the database - any better suggestions or how to get the current method working?

Once I've got this working, I'll change the MySQLi query to counteract injection (it's not live yet).

Upvotes: 1

Views: 93

Answers (2)

Rolley
Rolley

Reputation: 24

I think it the issue is with the sql query and wrapping the vehicle_id in quotes?

If you need to delete the row after you'll want to do it in a callback like this:

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

Upvotes: 0

CodeGodie
CodeGodie

Reputation: 12132

obtain your data attribute information using .data() . Also return your PHP results and dump it to the console. Lastly, check your console for errors. Use this instead:

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

Upvotes: 1

Related Questions