【问题标题】:delete selected rows from table php Angularjs从表php Angularjs中删除选定的行
【发布时间】:2019-11-13 23:22:13
【问题描述】:

我正在尝试使用 angularjs 和 php 从表中删除多行, 我面临的问题是我每次只能删除一行!

这是我的桌子:

<div class="table-responsive" style="overflow-x: unset;">           
    <button class="btn btn-danger" ng-click="deleteBulk()" >Delete</button>
    <table datatable="ng" dt-options="vm.dtOptions" class="table table-bordered table-striped" >
        <thead>
            <tr>                
                <th ><input type="checkbox" ng-model="master" > select</th>
                <th>date</th>
                <th>first name</th>
                <th>last name</th>
                <th>age</th>
                <th>action</th>
                <th>action</th>
                <th>action</th>
            </tr>
        </thead>
        <tbody>
            <tr id="<?php echo $row["id"]; ?>" ng-repeat="name in namesData">
                <td name="checkbox">
                  <input type="checkbox" name="arrExample"
                         ng-model="arrInput" ng-true-value="{{name.id}}"
                         ng-checked="master"
                         ng-click='pushInArray(name.id)'>
                </td>
                <td>{{name.date}}</td>
                <td>{{name.first_name}}</td>
                <td>{{name.last_name}}</td>
                <td>{{name.age}}</td>
                <td><button type="button" ng-click="fetchSingleData(name.id)"
                            class="btn btn-warning btn-xs">edit</button></td>
                <td><button type="button" ng-click="deleteData(name.id)"
                            class="btn btn-danger btn-xs">delete</button></td>
                <td><button type="button" ng-click="single_move(name.id)"
                            class="btn btn-success btn-xs">send</button></td>            
            </tr>
        </tbody>            
    </table>

这是我的 Angular 代码:

$scope.exampleArray = [];
$scope.pushInArray = function(id) {
     // get the input value
     var inputVal = id;
     var array = $scope.exampleArray.push(inputVal);
     $scope.deleteBulk = function(){
        if(confirm("Are you sure you want to delete this record?")){
             $http({
                 method:"POST",
                 url:"insert.php",
                 data:{'id':id, 'action' : 'deleteBulk'}
             }).success(function(data){
                    $scope.success = true;
                    $scope.error = false;
                    $scope.successMessage = data.message;
                    $scope.fetchData();
             });
        }
    };   
};

这是我在 insert.php 中的 php 代码

if($form_data->action == "deleteBulk")
{
    $query = "
     DELETE FROM orders WHERE id='".$form_data->id."'
    ";
    $statement = $connect->prepare($query);
    if($statement->execute())
    {
        $output['message'] = 'done!';
    }
}

谁能告诉我如何解决它?

谢谢

【问题讨论】:

  • 您在哪里声明要删除多行?基本上有两种方法可以做到这一点。 DELETE FROM TABLE WHERE id IN (1,2,3)foreach 语句,每行带有单独的 delete 查询。
  • 我怎么能用 foreach 语句做到这一点?

标签: php mysql angularjs


【解决方案1】:

不要在另一个作用域函数中定义作用域函数:

错误

$scope.exampleArray = [];
$scope.pushInArray = function(id) {
     // get the input value
     var inputVal = id;
     var array = $scope.exampleArray.push(inputVal);
     $scope.deleteBulk = function(){
        if(confirm("Are you sure you want to delete this record?")){
             $http({
                 method:"POST",
                 url:"insert.php",
                 data:{'id':id, 'action' : 'deleteBulk'}
             }).success(function(data){
                    $scope.success = true;
                    $scope.error = false;
                    $scope.successMessage = data.message;
                    $scope.fetchData();
             });
        }
    };   
};

更好

$scope.exampleArray = [];
$scope.pushInArray = function(id) {
    // get the input value
    var inputVal = id;
    $scope.exampleArray.push(inputVal);
};

$scope.deleteBulk = function(){
    if(confirm("Are you sure you want to delete this record?")){
         $http({
             method:"POST",
             url:"insert.php",
             data:{'id':id, 'action' : 'deleteBulk'}
         }).then(function(response){
             var data = response.data;   
             $scope.success = true;
             $scope.error = false;
             $scope.successMessage = data.message;
             $scope.fetchData();
         });
    }
};   

【讨论】:

  • 当我使用你的代码时,当我点击提交按钮时没有任何反应
猜你喜欢
  • 2016-10-02
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
  • 2014-07-24
  • 2015-05-04
相关资源
最近更新 更多