2016-08-09 87 views
3

Ich baue ein Projekt mit eckigen und PHP. Ich habe eine "Datei" -Tabelle in meiner Datenbank, die ich Datei senden und alle Informationen abrufen kann, die ich brauche. Ich habe einen Lösch-Button hinzugefügt, aber ich weiß nicht, warum es nicht funktioniert. Es gibt keine Fehler in meiner Konsole. Kann mir bitte jemand meinen Code ansehen?Löschen funktioniert nicht von der Datenbank

PHP für deleteing:

<?php 

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

include_once 'file.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(); 
$data = json_decode(file_get_contents("php://input")); 
$x = $data->reffer_customer_id; 

$reffer_customer_id = $data->reffer_customer_id; 
$del = "DELETE FROM file WHERE reffer_customer_id = " . $reffer_customer_id; 
//echo $del; 
mysqli_query($connect, $del); 
$newURL = "/hamatkin/#/allPriceOffers"; 

header('Location: '.$newURL); 

?> 

Controller:

$scope.delete = function(deletingId, $index) { 

    $http.post('api/customers-tab/delete-priceOffer.php', { "reffer_customer_id" : deletingId }) 
     .success(function(data) { 

      var arr = JSON.parse(JSON.stringify(data)); 
      $scope.files = arr; 
      var arr2 = arr.split(","); 
      arr2.splice($index, 1); 
      $route.reload(); 
     }); 
    }; 

Html Schaltfläche Löschen:

<td><a ng-click="delete(x.reffer_customer_id, $index)" class="btn btn-primary btn-active">מחיקה</td> 
+0

wird der erzeugte HTML tatsächlich einen Wert für '$ index' haben? – RST

+0

führen Sie Ihre generierte Raw-SQL-Abfrage direkt an phpmyadmin –

+0

@RST ja in den Quellen kann ich sehen, dass der $ index den Wert, aber die "deletingId" nicht – tanyaa

Antwort

-2

Ich denke, Ihre Abfrage wie diese sein sollte:

$del = "DELETE FROM file WHERE reffer_customer_id=".$reffer_customer_id." "; 
+0

Als erstes sollten Sie keine Variablen von "non-secure-outside" verwenden, ohne zu entkommen. Es ist eine Möglichkeit, über SQL-Injection gehackt zu werden. Stellen Sie sich vor, was Ihr SQL tun wird, wenn jemand "OR 1 = 1; usw. übergeben wird. Und zweitens - das Symbol der doppelten Anführungszeichen in PHP stellt Verarbeitungsvariablen im Inneren bereit, Sie sollten sie nicht verketten. –

+0

danke für den Rat, ich bin nur ein Neuling in Danke, dass Sie Ihr Wissen geteilt haben –

0

Zunächst sollten Sie Ihre HTML-fix:

  1. Ich weiß nicht, was Sie Funktion auf ng-click Attribut löschen bedeuten setzen, wahrscheinlich wollen Sie on-click stattdessen verwenden? Korrigieren Sie mich, wenn ich falsch
  2. Sie haben <a> Tag geöffnet, aber </a> fehlt

Korrigierte HTML-Schaltfläche in <td> ist:

<td><a on-click="delete(x.reffer_customer_id, $index); return false;" class="btn btn-primary btn-active">מחיקה</a></td> 

Im zweiten schlage ich vor, Ihr MySQL-Schema zu überprüfen der Tabelle file und stellen Sie sicher, dass Sie denselben Typ refferal_customer_id angeben. Zum Beispiel habe ich habe mich gefragt, dass diese numerischen Wert sein sollte, als:

<?php 

    header('Content-Type: text/html; charset=utf-8'); 

    /* 
    * I propose to check all data before you will open connection to MySQL, 
    * because if data is not correct - connection will not be used 
    * I strongly propose to process errors in your client scripts 
    */ 
    $data = json_decode(file_get_contents("php://input")); 
    if (!isset($data) || 
     !isset($data->reffer_customer_id) || 
     !is_numeric($data->reffer_customer_id) 
    ) { 
     echo 'Incorrect data specified'; 
     exit; 
    } 

    /* 
    * Connecting 
    */   
    $connect = mysqli_connect("localhost", "root", "", "hamatkin"); 

    /* 
    * I don't know why you using it for delete query if your id is numeric 
    */ 
    mysqli_query($connect, "SET character_set_client = utf8"); 
    mysqli_query($connect, "SET character_set_connection = utf8"); 
    mysqli_query($connect, "SET character_set_results = utf8"); 

    /* 
    * I don't recommend you to pass mysql connection error in raw format, 
    * because it can be used against you 
    * And don't forget to halt your script if error occurred 
    */ 
    if (mysqli_connect_errno()) { 
     echo "Failed to connect to MySQL"; 
     exit; 
    } 

    /* 
    * Here your delete query 
    */ 
    $delId = mysqli_real_escape_string($connect, $data->reffer_customer_id); 
    if (!$delId) { 
     echo 'Incorrect id for delete query specified'; 
     exit; 
    } 

    /* 
    * Don't forget to check errors 
    */ 
    if (!mysqli_query($connect, "DELETE FROM file WHERE reffer_customer_id = $delId")) { 
     echo "Failed to delete reffer_customer with id: $delId"; 
     exit; 
    } 

    /* 
    * And close the connection 
    */ 
    mysqli_close($connect); 

    /* 
    * As for me: better to put next route into response for redirecting from client 
    * but I don't know what you will do with this, so, putting header 
    */ 
    header('Location: /hamatkin/#/allPriceOffers');