This is my first Question here so i try to keep it short.
thats my angular method:
delete(id: number): Observable<User[]> {
console.log(id);
return this.http.delete(`${this.baseUrl}/deleteUser`)
.pipe(map(res => {
const filteredUsers = this.users.filter((user) => {
return +user['id'] !== +id;
});
return this.users = filteredUsers;
}),
catchError(this.handleError));
}
i logged the id so i know it works so far, but everything after the console. log isn´t working
my api looks like this:
require 'connect.php';
// Extract, validate and sanitize the id.
$tp = ($_GET['id'] !== null && (int)$_GET['id'] >= 0)? mysqli_real_escape_string($con, (int)$_GET['id']) : false;
$id = (int)$tp;
var_dump($id);
if(!$id)
{
return http_response_code(400);
}
// Delete.
$sql = "DELETE FROM `user_items` WHERE `user_items_id` ='{$id}' LIMIT 1";
if(mysqli_query($con, $sql))
{
http_response_code(204);
}
else
{
return http_response_code(422);
}
When i type localhost/api/deleteUser.php=?18 for example, it deletes the user with user id 18 successfully.
Im suing this requests multiple times in my app and it works everywhere else. I just copy pasted it in and changed my class/array/object names.
Can someone point out my error or give me an example of an alternative approach?
This is what my .htaccess looks like
# Remove the php extension from the filename
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# Set the headers for the restful api
Header always set Access-Control-Allow-Origin http://localhost:4200
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT, UPDATE"
And my connect.php that i require in all my apis
<?php
// db credentials
define('DB_HOST', 'localhost');
define('DB_USER', 'example');
define('DB_PASS', 'example');
define('DB_NAME', 'example');
// Connect with the database.
function connect()
{
$connect = mysqli_connect(DB_HOST ,DB_USER ,DB_PASS ,DB_NAME);
if (mysqli_connect_errno($connect)) {
die("Failed to connect:" . mysqli_connect_error());
}
mysqli_set_charset($connect, "utf8");
return $connect;
}
$con = connect();