【问题标题】:How to delete row with jquery, ajax and PDO如何使用 jquery、ajax 和 PDO 删除行
【发布时间】:2017-04-29 18:42:27
【问题描述】:

我在 mysql 数据库中有两个表,“汽车”和“地址”,它们与主键和数据库中的“删除限制”有关系,所以我不能删除一些有汽车的地址,这很好。 DB 中的地址有“street”、“number”和“city”列。我想删除每个没有汽车的位置,但我不知道我的代码哪里错了,它不起作用。我检查了所有路径,一切正常。任何建议表示赞赏。

这是我的模态删除按钮:

<a href="#" data-toggle="modal" data-target="#delete_loc<?php echo $value['id']?>" role="button" class="btn btn-danger btn-sm">
                <i class="fa fa-trash-o"></i> Delete
            </a>

                <div class="modal fade" id="delete_loc<?php echo $value['id']?>" tabindex="-1" role="dialog" aria-labelledby="mydelModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content" style="text-align:center;">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                                <h2 class="modal-title" id="mydelModalLabel" style="color:#a80b27;text-transform:uppercase;font-size:1.6rem;">Warning   !</h2>
                            </div>
                            <div class="modal-body">
                                <h5>Are you shure you want delete address ID <b><?php echo $value['id']?></b>?</h5>
                            </div>
                            <div class="modal-footer">
                            <input type="hidden" name="loc_id" id="loc_id" value="<?php echo $value['id']?>">
                                <a href="" role="button" class="btn btn-danger" id="del_loc">
                                    Yes, I'm shure!
                                </a>
                                <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
                            </div>
                        </div>
                    </div>
                </div>

这是 jquery 和 ajax:

    $(document).ready(function() {
      $('#del_loc').bind('click',function(e){
      e.preventDefault();
        var id = $('#loc_id').val();

        var data = 'loc_id='+id;

     $.ajax({
       type: "POST",
       url: "include/locations/delete.php",
       data: data,
       success: function(result){
        if(result == 1){
          window.location.replace("address.php?id=");
    }
       else{
         err_msg = JSON.parse(result);
       if(err_msg.nAddress){
        $('.panel-footer').append('<span class="help-block"  style="float:right;"><strong>'+err_msg.nAddress+'</strong></span>');
        }
      }
    }
   });
  });
});

这是delete.php:

    <?php

     require '../../config/init.php';
     require '../functions/locations.php';
     require '../services/xss.php';

     $loc_id = _e($_POST['loc_id']);

     $data = deleteLoc($loc_id);

       if(is_array($data)){
        print_r(1);
       }
       else{
          echo $data;
       }

  ?>

这是一个名为locations.php的函数:

function deleteLoc($loc_id){
    global $conn;

            try{
                $stmt = $conn->prepare('DELETE FROM address WHERE id=:id');
                $stmt->bindParam(':id',$loc_id);                
                $stmt->execute();
                $row[] = 'deleted';
                return $row;                
            }
            catch(PDOException $e){
                if(DEBUG === true){
                    header('Location:../../error/db_error.php?err_msg='.$e->getMessage());
                }
                else{
                    header('Location:../../error/db_error.php?err_msg');
                }
        }   
}

【问题讨论】:

  • 尝试使用软删除而不是硬删除。这样,您在从 MySql 中删除时不会遇到任何冲突。在您的表中添加一个名为已删除的列,其数据类型为 tinyint & onclick 删除按钮将已删除标志设置为 1。
  • 您说您想要删除所有没有汽车的位置,但您的SQL 只是'DELETE FROM address WHERE id=:id',它只会从address 表中删除一行(假设id 是它的主键)。您的代码没有任何内容表明它应该删除所有没有汽车的位置
  • @KunalGadhia 我不确定你想说什么。使用软删除与这个问题有什么关系?
  • @DelightedD0D 我的错误是因为我没有告诉我在一页上显示了所有地址,并且在每个地址的末尾我有每个行/地址的模式删除按钮,应该删除该行/地址.但是这段代码不起作用,当我尝试删除除了关闭模式窗口之外的某些行时,数据库中什么也没有发生。
  • 在ajax调用前添加console.log(data);,控制台的输出是什么?

标签: php jquery mysql ajax pdo


【解决方案1】:

如果你正确地获得了loc_id 的值。我认为下面的代码会对你有很大帮助。让我们试试..

JavaScript

<script type="text/javascript">
     $(document).ready(function() {
      $('#del_loc').bind('click',function(e){
      e.preventDefault();
        var id = $('#loc_id').val();

        var data = 'loc_id='+id;
     //try here alert(id); For checking whether you are getting value or not
     $.ajax({
       type: "POST",
       url: "include/locations/delete.php",
       dataType:'JSON',
       data: data,
       success: function(result){
        var res=eval(result);
        if(res.success){
          window.location.replace("address.php?id=");
               }
       else
       {
       if(res.nAddress){
        $('.panel-footer').append('<span class="help-block"  style="float:right;"><strong>'+err_msg.nAddress+'</strong></span>');
        }
      }
    }
   });
  });
});
</script>

PHPdelete.php

<?php

     require '../../config/init.php';
     require '../functions/locations.php';
     require '../services/xss.php';

     $loc_id = _e($_POST['loc_id']);

     $data = deleteLoc($loc_id);

       if(is_array($data)){
        echo json_encode(array('success'=>true));
       }
       else{
          echo json_encode($data);
       }
  ?>

【讨论】:

  • 为什么你认为这会有所帮助?
  • 是的任何错误评论请在 ajax 和 jquery 中新增。
  • 不,我的意思是我看不出有理由相信在输入上调用 mysqli_real_escape_string 会对这个问题产生任何影响(除非 OP 在他们的 ID 中有撇号等值,这是极不可能的)。
  • 另外,OP 已经在使用的PDO::prepare()PDOStatement::execute() 将自动处理任何需要的转义,所以这不是这里的问题。见stackoverflow.com/a/3716402/1376624
猜你喜欢
  • 2014-10-05
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
  • 2016-10-24
  • 2018-10-20
相关资源
最近更新 更多