Woonghee Woonghee - 1 year ago 155
Ajax Question

Delete function in php using ajax

I am trying to make a dynamic table with ajax/jquery.

other functions such as add, update are working but my delete function seem to be not working. I am digging into this problem but with no result.

Below is the snippet which fetches data from the database and form up a table.
Each row has update/delete buttons which lead into appropriate functions.

if(mysqli_num_rows($result) > 0)
$number = 1;
while($row = mysqli_fetch_assoc($result))
$data .= '<tr>
<button onclick="GetUserDetails('.$row['id'].')" class="btn btn-warning">Update</button>
<button onclick="DeleteUser('.$row['id'].')" class="btn btn-danger">Delete</button>

here is my delete function it receives the id of the row from the other php where it loads table from database.

Delete Function script

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

deletefunction php

// check request
if(isset($_POST['id']) && isset($_POST['id']) != "")
// include Database connection file

// get user id
$client_id = $_POST['id'];

// delete User
$query = "DELETE FROM Clients WHERE id = '$client_id'";
if (!$result = mysqli_query($query)) {

I even tried going into mysql instead of mysqli, and it somehow worked. I am kinda assuming that mysqli has different function than mysql? Well I heard mysql is deprecated but it seems I am doing wrong something here...

I am not good at programming, I am learning as it became my new hobby. Any tips would be thankful.

Answer Source

mysqli_* is more object oriented and imporved version of MySQL.

You need to pass connection id to mysqli_query()

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

So, the corrected code:

if (!$result = mysqli_query($conn, $query)) {
// Where $conn is the connection identifier.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download