0

I want to create delete feature using function and jquery

My jquery works and show messages but nothing happen "Nothing Deleted"

Jquery Code

<script type="text/javascript">
$(".remove").click(function(){
    var id = $(this).parents("tr").attr("id");

    if(confirm('Are you sure to remove this record?'))
    {
        $.ajax({
           url: 'delete.php',
           type: 'GET',
           data: {id: id},
           error: function() {
              alert('Something is wrong');
           },
           success: function(data) {
                $("#"+id).remove();
                alert("Record removed successfully");
           }
        });
    }
});

PHP Function Code

function delete($table,$id) {
    global $connect;
    mysqli_query($connect, "DELETE FROM `$table` WHERE `id` = $id ");
}

Delete.php Code

include ('function.php');
$id = $_GET['id'];
$table = 'msg';
delete($table,$id);

HTML Code

<table class="table table-striped" style="background-color: #ffffff;">
    <tr>
        <th>ID</th>
        <th>From</th>
        <th>Title</th>
        <th>Date</th>
        <th>Action</th>
    </tr>
    <?php
    $i        = '1';
    $username = $user_data['username'];
    $query    = "SELECT * FROM msg WHERE `go_to` = '$username' Order by id";
    $result   = mysqli_query($connect, $query);

    while($row = mysqli_fetch_assoc($result))
    {
        ?>
        <tr>
            <td><?php echo $i++; ?></td>
            <td><?php echo $row['come_from']; ?></td>
            <td>
                <a href="read_message/<?php echo $row['id']; ?>"><?php if(count_msg_not_opened($username, $row['id']) > '0')
                    {
                        echo $row['title'];
                    }
                    else
                    {
                        echo '<b>' . $row['title'] . '</b>';
                    } ?></a></td>
            <td><?php echo $row['date']; ?></td>
            <td>
                <button class="btn btn-danger btn-sm remove">Delete</button>
            </td>
        </tr>
    <?php } ?>
</table>

I also include "jquery.min.js"

When I press "Delete" bottom this message appears "Are you sure to remove this record?"

I pressed "Yes" then this message appears "Record removed successfully", but nothing was deleted.

I don't know where the problem is.

5
  • There's nothing in the PHP code that checks whether the DELETE was successful or that anything was deleted. Commented Sep 26, 2017 at 18:21
  • You can do even better. If you pass 0; drop table msg; as the id, all your rows will be deleted at once! Don't use mysqli. Ever. Use PDO instead, and read up on SQL injections. php.net/manual/en/book.pdo.php Commented Sep 26, 2017 at 18:23
  • @Peter No it won't. You would have to call mysqli_multi_query() for that to work. Commented Sep 26, 2017 at 18:25
  • Thanks a lot that's works .. sorry little expert in jquery Commented Sep 26, 2017 at 18:25
  • Barmar, semantics. It's still trivial to abuse the query. Commented Sep 26, 2017 at 18:27

2 Answers 2

1

You forgot to add the id attribute to the <tr>

<tr id="<?php echo $row['id']; ?>">

You should also add error checking and prepared statements to your PHP code.

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

Comments

0

Are you sure that you have connected your PHP-code to your SQL-database?

function delete($table,$id) {
global $connect;
mysqli_query($connect, "DELETE FROM `$table` WHERE `id` = $id ");
}

The code above is relying on a connection already existing within your PHP-file. See this to find out how to apply a connection.

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.