Как удалить строку базы данных с Angular и PHP?

0

Я строю проект с угловым и php. У меня есть html-страница, отображающая список клиентов из таблицы "Клиенты". Я пытаюсь удалить строку, нажав кнопку "удалить", она не удаляет и не показывает никаких ошибок. может ли кто-нибудь помочь?

это мой код:

контроллер:

  $scope.delete = function(deletingId, $index) {
        var params = $.param({"customer_id":deletingId});
        $http({
          headers: {'Content-Type': 'application/x-www-form-urlencoded'},
          url: 'http://localhost:8081/hamatkin/api/delete-customer.php/',
          method: "GET",
          data: params
        }).success(function(data){



          // var arr=JSON.parse(JSON.stringify(data));
          // console.log(data);
           var arr=JSON.parse(JSON.stringify(data));
            $scope.customerDetails = arr;
             var arr2 = arr.split(",");
              arr2.splice($index, 1);


        });
      }

Php- delete-customer.php

<?php header('Content-Type: text/html; charset=utf-8');
$connect=mysqli_connect("localhost", "root", "", "hamatkin");

  include_once 'Customer.php';
mysqli_query($connect,"SET character_set_client = utf8");
mysqli_query($connect,"SET character_set_connection = utf8");
mysqli_query($connect,"SET character_set_results = utf8");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$customer = new Customer();


if(isset($_GET['customer_id'])){
   $customer_id = $_GET['customer_id'];
   $del = "DELETE FROM posts WHERE customer_id='".$customer_id."'";
   mysql_query($connect, $del);
 }


$newURL = "/hamatkin/#/customerCards";
header('Location: '.$newURL);
?>

HTML

  <tr ng-repeat="x in customers  | filter:search_query | orderBy: order_query:reverse_query">

            <td>{{ x.customer_id}}</td>
            <td>{{ x.kind_Of_Customer}}</td>
            <td>{{ x.full_name}}</td>
            <td><a ng-click="delete(x.customer_id, $index)" class="btn btn-primary btn- active">מחיקה</td>
  • 0
    Могу ли я узнать, почему вы передаете $ index в ng-click = "delete (x.customer_id, $ index?"
  • 0
    @vishal что ты предлагаешь делать?
Показать ещё 13 комментариев
Теги:

2 ответа

0

попробуйте понять простую концепцию удаления строк с угловыми

    <html ng-app="mainApp">
      <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
      <script>
         angular.module('mainApp').controller('myCtrl',function($scope,$http){
            //getting the table data from this http from database
               $http({
                  method : "GET",
                  url : "http://localhost/em/mod.php/info",
                  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                  }).then(function mySucces(response) {
                  $scope.mydata=response.data; 
                  }, function myError(response) {
                  $scope.mydata=response.statusText;
               });
                  $scope.delete=function(idd)
      {        

            //here delete passes the id to the php by which we will delete the row
            $http({
               method : "DELETE",
               url : "http://localhost/em/mod.php/info",
               data:{
               "id":idd,
               },
               headers: {'Content-Type': 'application/x-www-form-urlencoded'}
               }).then(function mySucces(response) {
                  $http({                 //here we are refreshing the table data
                     method : "GET",
                     url : "http://localhost/em/mod.php/info"
                     }).then(function mySucces(response) {
                     $scope.mydata=response.data;
                     }, function myError(response) {
                     $scope.mydata=response.statusText;
                  });
               }, function myError(response) {
               $scope.mydata=response.statusText;
            });
      }

      </script>
      //suppose we got the table data in $scope.mydata
      </head>
      <body  ng-controller="myCtrl">

         //Now below table we are showing our data and its id is passing in <a> delete when click press it send its id to the delete function 

          <table border="1">
               <tr>
                  <th>Predefine Message</th>
                  <th>CreatedBy</th>
                  <th>Status</th>
                  <th>Action</th>
               </tr>
               <tr ng-repeat="rs in mydata"> 
                  <td>
                     {{rs.message}}
                  </td>
                  <td>
                     {{rs.createdby}}
                  </td>
                  <td>
                     Active
                  </td>
                  <td>
                     <a href="" ng-click="delete(rs.id)">Delete</a>
                  </td>
               </tr>
            </table> 



      </body>
   </html>
0

Я заметил, что вы используете mysqli для подключения db, тогда вы используете mysql для выполнения запроса, вы должны использовать тот же lib, попробуйте выполнить ваш запрос через mysqli_query()

  • 0
    спасибо, я перешел на mysqli_query ($ connect, $ del); но все тот же
  • 0
    ../delete-customer.php?customer_id=1 браузер отправляет какой-либо запрос, например ../delete-customer.php?customer_id=1 ?
Показать ещё 3 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню