如何在PHP Api中从Angular DELETE方法读取url id参数

vwkv1x7d  于 2022-12-02  发布在  PHP
关注(0)|答案(1)|浏览(106)

我正在使用删除方法在Angular 和发送ID参数到PHP REST API,但它是返回空值。那么我如何在PHP端读取参数?

删除.php

<?php
include('connection.php');

$extract = json_decode(file_get_contents("php://input"));
$idCourse = $extract->courses->idCourse;

$sql = "DELETE FROM courses WHERE idCourse='$idCourse'";
$conn->query($sql);
?>

课程.组件.html

<form>
  <label for='courseName'>Course name:</label>
  <input type="text" name="courseName" [(ngModel)]="course.courseName" />
  <label for='coursePrice'>Course price:</label>
  <input type="text" name="coursePrice" [(ngModel)]="course.coursePrice" />
  <button (click)="register(this.course)">Register</button>
  <button (click)="update(this.course)">Update</button>
  <button (click)="delete(this.course)">Delete</button>
</form>

<table border="1">
  <tr>
    <td>Course Name</td>
    <td>Price</td>
  </tr>
  <tr *ngFor='let course of courses'>
    <td>{{ course.courseName }}</td>
    <td>{{ course.coursePrice }}</td>
    <td><button (click)="listOne(course)">Select course</button></td>
  </tr>
</table>

列表一()

listOne(course: Course) {
    this.course.idCourse = course.idCurso;
    this.course.courseName = course.courseName;
    this.course.coursePrice = course.coursePrice;
  }

课程.组件.ts

delete(course: Course) {
    this.courseService.delete(course).subscribe((res: Course[]) => {
      this.courses = res;
      this.course = new Course();
      this.list();
    });
  }

课程服务ts

delete(course: Course): Observable<Course[]> {
    const params = new HttpParams().set('idCourse', course.idCourse!.toString());
    return this.http.delete(`${this.url}delete.php`, { params: params }).pipe(map((res: any) => {
      this.courses.filter((c) => c.idCourse !== curso.idCourse);
      return this.courses;
    }))
  }

PHP错误

[Wed Nov  9 18:13:12 2022] PHP Warning:  Attempt to read property "courses" on null in /var/www/api/backend/delete.php on line 5
[Wed Nov  9 18:13:12 2022] PHP Warning:  Attempt to read property "idCourses" on null in /var/www/api/backend/delete.php on line 5

PS:其他方法,如POST PUT和GET,使用PHP中类似的JSON读取方法(json_decode),工作正常。只有DELETE方法返回空值。

mcdcgff0

mcdcgff01#

You are adding the QueryParam idCourse to your request.
In your php code you are using $extract = json_decode(file_get_contents("php://input")); which will read the request body (not the query params) and parse it. Since you didn't pass a request body from angular (and probably shouldn't since this is a DELETE call, you should instead read the actual query param on server side.
There are different options to actually read a query string in PHP (see here: Get URL query string parameters ), but here is an example:

$queries = array();
parse_str($_SERVER['QUERY_STRING'], $queries);
$idCourse = $queries['idCourse']

$sql = "DELETE FROM courses WHERE idCourse='$idCourse'";
$conn->query($sql);

Big warning

You are basically making your application vulnerable to SQL injections if you directly pass your query param to an SQL query. You should take a look at https://www.php.net/manual/en/security.database.sql-injection.php to see how you can avoid this.

相关问题