Delete function in php using ajax

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 code

here 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 code

deletefunction php

<?php   
// check request   
if(isset($_POST['id']) && isset($_POST['id']) != "")   
{   
    // include Database connection file   
    include("db_connection.php");   

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

    // delete User   
    $query = "DELETE FROM Clients WHERE id = '$client_id'";   
    if (!$result = mysqli_query($query)) {   
        exit(mysqli_error($result)); 
    }   
}   
?>   
Run code

I 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 ...

, , . .

+4
2

mysqli_* - - MySQL.

mysqli_query()

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

, :

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

+3

.

    // delete User  
    $query = "DELETE FROM Clients WHERE id = '$client_id'";  
   if (!$result = mysqli_query($GLOBALS["___mysqli_ston"], $query)) { 
        exit(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); 
    } 
0

Source: https://habr.com/ru/post/1659785/


All Articles