4

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

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

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

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.

10
  • 1
    Try to change if(isset($_POST['id']) && isset($_POST['id']) != "") to if(isset($_POST['id'])) Commented Nov 4, 2016 at 4:33
  • @RazibAlMamun Tried that before, didnt work. Thank you anyway for the tips Commented Nov 4, 2016 at 4:35
  • So the request makes it to deletefunction.php but the mysql query fails? You are correct, don't use mysql_* functions. This is open to SQL injections currently. Commented Nov 4, 2016 at 4:35
  • Does db_connection.php open a mysqli or mysql_* connection? The mysqli_query requires the connection string as its first parameter. See php.net/manual/en/mysqli.query.php. Also @RazibAlMamun is correct. isset will never return '', you probably could use if(!empty(trim($_POST['id']))) {. Commented Nov 4, 2016 at 4:36
  • Everything is on mysqli at the moment Commented Nov 4, 2016 at 4:37

2 Answers 2

3

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.

Documentation

Sign up to request clarification or add additional context in comments.

Comments

0

This query worked for me at last.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.