I am trying to create a dynamic table using ajax / jquery.
other functions like add, update work, but my delete function doesn't seem to work. I dig this problem, but with no result.
Below is a fragment that extracts data from a database and forms a table. Each line has update / delete buttons that lead to the corresponding functions.
if(mysqli_num_rows($result) > 0)
{
$number = 1;
while($row = mysqli_fetch_assoc($result))
{
$data .= '<tr>
<td>'.$number.'</td>
<td>'.$row['Surname'].'</td>
<td>'.$row['Name'].'</td>
<td>'.$row['Address'].'</td>
<td>'.$row['Telephone'].'</td>
<td>'.$row['PurchaseDate'].'</td>
<td>'.$row['Model'].'</td>
<td>'.$row['SerialNumber'].'</td>
<td>'.$row['Notes'].'</td>
<td>
<button onclick="GetUserDetails('.$row['id'].')" class="btn btn-warning">Update</button>
</td>
<td>
<button onclick="DeleteUser('.$row['id'].')" class="btn btn-danger">Delete</button>
</td>
</tr>';
$number++;
}
}
Run codehere is my delete function, it gets the row id from another php where it loads the table from the database.
Script delete function
function DeleteUser(id) {
var conf = confirm("Are you sure, do you really want to delete User?");
if (conf == true) {
$.post("ajax/deleteUser.php", {
id: id
},
function (data, status) {
// reload Users by using readRecords();
readRecords();
}
);
}
}
Run codedeletefunction php
<?php
if(isset($_POST['id']) && isset($_POST['id']) != "")
{
include("db_connection.php");
$client_id = $_POST['id'];
$query = "DELETE FROM Clients WHERE id = '$client_id'";
if (!$result = mysqli_query($query)) {
exit(mysqli_error($result));
}
}
?>
Run codeI even tried switching to mysql instead of mysqli and it somehow worked. I seem to assume that mysqli has a different function than mysql? Well, I heard mysql is deprecated, but it seems like I'm doing something wrong ...
, , . .