简体   繁体   English

如何在Codeigniter中使用AJAX和PHP从MySQL数据库中删除记录

[英]How to delete a record from MySQL database using AJAX and PHP in Codeigniter

I want to delete records from a MySQL database table using AJAX. 我想使用AJAX从MySQL数据库表中删除记录。 I have done it with PHP and work fine. 我用PHP完成它并且工作正常。 But I've not been able to get it through with AJAX. 但我无法通过AJAX实现这一目标。

AJAX AJAX

$(document).ready(function() {
    $(".confirm").click(function() {
        var bid = $(this).closest("div.box2").find('input[name="dbid"]').val();
        var dataString = 'id=' + bid;
        $.ajax({
            type: "POST",
            url: "<?php echo site_url('user/delete_article')?>",
            data: dataString,
            cache: false,
            success: function() {

                $.alert('Confirmed!');
            }
        });
    });
}); 

PHP PHP

public function delete_article($id){
  $data['success']='';
  $data['error']='';
  include_once ('query/user_query.php');  
  $this->db->where('bid', $id);
  $data['countEarticle'] = $this->db->count_all_results('blog');
  if($data['countEarticle'] >= 1){

      $this->db->where('bid',$id);
      $this->db->delete('blog');

  }
  if($data['countEarticle'] <= 0){

  }          
}

HTML HTML

<div class="box-footer box-comments box2" style="display: block;">
    <input type="hidden" name="dbid" value="<?php echo $draftfull['bid']?>">
    <p>
        <btn class="btn btn-azure btn-sm confirm"><i class="fa fa-trash-o"></i>Delete Article</btn>
    </p>
</div>

I need your help. 我需要你的帮助。 What am I doing wrong? 我究竟做错了什么?

try this code:- 试试这段代码: -

public function delete_article(){
    $id=$this->input->post('bid');
    $data['success']='';
    $data['error']='';
    include_once ('query/user_query.php');

    $this->db->where('bid', $id);
    $data['countEarticle'] = $this->db->count_all_results('blog');
    if($data['countEarticle'] >= 1){

        $this->db->where('bid',$id);
        $this->db->delete('blog');

    }
    if($data['countEarticle'] <= 0){

    }          
   }

try this: 尝试这个:

<a href="javascript:;" class="btn btn-danger btn-xs mt-sweetalert swtalert" onclick="delete('<?php echo $draftfull["bid"] ?> ')" title="Delete"><span class="fa fa-ban"></span></a>

//ajax // AJAX

function delete(id) {
   swal({
        title: "Are you sure to delete?",
        text: "Deleting will remove row from listing!",
        type: "error",
        showCancelButton: true,
        confirmButtonClass: "btn-danger",
        confirmButtonText: "Yes!",
        cancelButtonText: "No",
        closeOnConfirm: true,
        closeOnCancel: true
    }, function (isConfirm) {
        if (isConfirm) {
            $.post(
                base_url + "user/delete_article", 
                {bid: id}, 
                function (data) {
                    if (data === "1") {
                        location.reload();
                    } else if (data === "0") {
                        swal("", "Error to deleting data.", "warning");
                    } else {
                        swal("", data[0], "error");
                    }
                });
            }
        });
}

//Controller //控制器

function delete_article($id){
    if ( $this->model_name->deleteDataById($this->input->post('bid') ) {
        die('1');
    }
    die('0');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM