I'm building a project with angular and php, I have a "Customers" table wich I can retrieve all data, but I have a problem with deleting one row. if the "Customer" has "Orders", I can't delete the (row)"Customer". if the "Customer" dosn't have "orders", I can delete with no problem.this is the error that I get on phpmyadmin=#1451 - Cannot delete or update a parent row: a foreign key constraint fails. can anyone help?
php code for deleting:
<?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();
}
$data = json_decode(file_get_contents("php://input"));
$x=$data->customer_id;
$customer_id = $data->customer_id;
$del = "DELETE FROM customers WHERE customer_id=".$customer_id;
mysqli_query($connect, $del);
}
$newURL = "/hamatkin/#/customerCards";
header('Location: '.$newURL);
?>
controller:
$scope.delete = function(deletingId, $index) {
var params = $.param({"customer_id":deletingId});
$http.post('api/customers-tab/delete-customer.php',{"customer_id":deletingId})
.success(function(data){
var arr=JSON.parse(JSON.stringify(data));
$scope.customerDetails = arr;
var arr2 = arr.split(",");
arr2.splice($index, 1);
$route.reload();
});
}
html code:
<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> {{ x.id}} </td>
<td> {{ x.city}} </td>
<td><a href="/hamatkin/index.html#/customerCardDetails/{{ x.customer_id}}" class="btn btn-primary btn- active">הצג פרטי לקוח </a></td>
<td><a ng-click="delete(x.customer_id, $index)" class="btn btn-primary btn- active">מחיקה</td>