This is my initial question, so I'll try to keep it concise.
Here is the Angular method that I am using:
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 have confirmed that the id is being logged correctly, but nothing seems to work after the console.log statement.
This is how my API is structured:
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);
}
By typing localhost/api/deleteUser.php=?18, for example, I can successfully delete the user with user id 18.
I am using these requests repeatedly in my application and they work everywhere except here. I simply copied and pasted the code and adjusted the necessary names.
If anyone can identify the mistake or provide an alternative approach, I would greatly appreciate it.
This is what my .htaccess file consists of:
# 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"
Lastly, here is my connect.php file that is required in all of 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();