0

I am deleting row in PHP using AJAX, but it shows Row can't delete. My Delete button is like this...

while ($row = mysqli_fetch_array($exec)) 
{
?>
    <tr>
        <td><?php echo $row["create_date"]; ?></td>
        <td><?php echo $row["bni_member_id"]; ?></td>
        <td><?php echo $row["bni_member_name"]; ?></td>
        <td><?php echo $row["bni_chapter_id"]; ?></td>
        <td><?php echo $row["bni_category_id"]; ?></td>
        <td><input type="button" name="delete" onclick="delFun()" value="delete" id="<?php echo $row["bni_member_id"]; ?>" class="btn btn-info btn-xs delete_data"/></td>
    </tr>
    <?php
}
?>

I call a function on the click button. Function like this. It always Executes part can't delete. I think there is a problem with

data: {del_id: del_id},

I think there is a problem with Data... but I can't resolve it.

   var delfin;
$(document).ready(function () {
    delFun = function () {
        $('.delete_data').click(function () {
            var del_id = $(this).attr("name");
            var $ele = $(this).parent().parent();
            $.ajax({
                url: "phpfile/delete.php",
                method: "POST",
                data: {del_id: del_id},
                success: function (data) {
                    if(data=="YES"){
                        $ele.fadeOut().remove();
                     }else{
                            alert("can't delete the row")
                     }
                }
            });
        });
    } 
}); 

And My PHP file is in another directory like this

<?php
    include('../../connection.php');
    $music_number = $_POST['del_id'];
    //echo $music_number;
    $qry = "DELETE FROM bni_member WHERE bni_member_id ='$music_number'";
    $result=mysql_query($qry);
    if(isset($result)) {
       echo "YES";
    } else {
       echo "NO";
    }
?>

My table is like

CREATE TABLE `bni_member` (
 `bni_member_id` int(11) NOT NULL AUTO_INCREMENT,
 `bni_member_name` text,
 `bni_member_mobile` varchar(13) DEFAULT NULL,
 `bni_member_email` text,
 `bni_member_website` text,
 `bni_member_bio` text,
 `bni_member_export_to` text,
 `bni_member_import_from` text,
 `bni_member_want_to_connect_to` text,
 `bni_member_company` text,
 `bni_chapter_id` int(11) DEFAULT NULL,
 `bni_category_id` int(11) DEFAULT NULL,
 `bni_member_address` text,
 `bni_member_commitee` text,
 `bni_member_profilepic` longblob NOT NULL,
 `bni_member_logo` longblob NOT NULL,
 `create_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
 `is_active` int(1) NOT NULL DEFAULT '1',
 `is_delete` int(1) NOT NULL DEFAULT '0',
 `last_update` text,
 `del_status` varchar(50) NOT NULL,
 PRIMARY KEY (`bni_member_id`),
 KEY `bni_chapter_id` (`bni_chapter_id`),
 KEY `bni_category_id` (`bni_category_id`),
 KEY `bni_member_id` (`bni_member_id`),
 CONSTRAINT `bni_member_ibfk_1` FOREIGN KEY (`bni_chapter_id`) REFERENCES 
`bni_chapter` (`bni_chapter_id`),
 CONSTRAINT `bni_member_ibfk_2` FOREIGN KEY (`bni_category_id`) 
REFERENCES `bni_category` (`bni_category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=225 DEFAULT CHARSET=latin1
sanjay sisodiya
  • 455
  • 4
  • 14
Meet Patel
  • 67
  • 7
  • don't use `mysql_query` it is deprecated now. – prakash tank Jul 31 '19 at 06:19
  • any error are you getting? – Pathik Vejani Jul 31 '19 at 06:20
  • your API usage is confusing, on the tables, you use `mysqli_fetch_array`, then on the PHP ajax processing, you use `mysql_query`, just use `mysqli` – Kevin Jul 31 '19 at 06:26
  • I updated this but still I gives row can't delete `$result=mysqli_query($qry); if(isset($result)) { echo "YES"; } else { echo "NO"; }` @Ghost – Meet Patel Jul 31 '19 at 06:34
  • No I am not getting any error @PathikVejani I updated `$result=mysqli_query($qry); if(isset($result)) { echo "YES"; } else { echo "NO"; }` – Meet Patel Jul 31 '19 at 06:43
  • 1
    check `var del_id = $(this).attr("name")` should be `var del_id = $(this).attr("id")` – Roy Bogado Jul 31 '19 at 06:44
  • I updated with `mysqli` `$result=mysqli_query($qry); if(isset($result)) { echo "YES"; } else { echo "NO"; }` @prakashtank – Meet Patel Jul 31 '19 at 06:45
  • I have changed to `var del_id = $(this).attr("id")` @Roy – Meet Patel Jul 31 '19 at 06:48
  • @MeetPatel and what happened? You are getting .attr wrong. – Pathik Vejani Jul 31 '19 at 06:51
  • It's because in your js `var del_id = $(this).attr("name");` return "delete" and it do not help for the query. You should add a data attribute on your button, like `data-id="` and then get it in JS via `var del_id = $(this).data('id');`. Also you should secure your SQL queries, and use PDO if possible. – Ersian Jul 31 '19 at 06:51
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Dharman Jul 31 '19 at 08:40

3 Answers3

3

The first problem is that, you're getting name attribute from your delete button. You can pass bni_member_id as a data attribute instead of using element id attribute. It can cause confusion.

And the second problem is that, you're using both onlick attribute and jQuery's click method. Using one them is pretty enough. Your input button will look like this:

<td><input type="button" name="delete" value="delete" class="btn btn-info btn-xs delete_data" data-bni-member-id="<?= $row["bni_member_id"]; ?>" /></td>

Then your js will look like this:

$(document).ready(function () {
    $('.delete_data').click(function () {
        var del_id = $(this).data("bni_member_id");
        var $ele = $(this).parent().parent();
        $.ajax({
            url: "phpfile/delete.php",
            method: "POST",
            data: {del_id: del_id},
            success: function (data) {
                if(data=="YES"){
                    $ele.fadeOut().remove();
                } else {
                    alert("can't delete the row")
                }
            }
        });
    });
});

I hope this helps you.

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47
0

you can change delete button

 <input type="button" name="delete" onclick="delFun()" value="delete" data-id="<?php echo $row["bni_member_id"]; ?>" class="btn btn-info btn-xs delete_data"/>

And Jquery Event

    $(document).on("click",".delete_data",function () {

    var del_id = $(this).data("id");
Ritesh Dhoke
  • 169
  • 10
0

I am sharing my code with you try like this

       <tr>
          <td><?php echo $i; ?></td>
          <td><?php echo $row["exam_name"]; ?></td>
          <td><?php echo $row["exam_date"]; ?></td>
          <td><?php echo $row["exam_status"]; ?></td>
          <td><?php echo $row["exam_comment"]; ?></td>
         <td><button type="button" name="delete_btn" id="<?php echo $row["examid"]; ?>" class="btn btn-sm btn-danger btn_delete"><i class="fa fa-trash"></i></button></td> 
      </tr>

Add script

<script type="text/javascript">
    $(document).on('click', '.btn_delete', function(){  
           // var id=$(this).data("id3");
            var el = this;
        var id = this.id;
        var splitid = id.split("_");

        // Delete id
        var deleteid = splitid[1];  
           if(confirm("Are you sure you want to delete this?"))  
           {  
                $.ajax({  
                     url:"path.....",  
                     method:"POST",  
                     data:{id:id},  
                     dataType:"text",  
                     success:function(data){  
                           $(el).closest('tr').css('background','#d31027');
                $(el).closest('tr').fadeOut(800, function(){      
                    $(this).remove();
                });  

                     }  
                });  
           }  
      }); 
 </script>