2024/8/1 13:32:45
我的classes.php代码如下,似乎没有编写修改逻辑和删除逻辑,请帮我添加:
<?php
require_once 'init.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$data = json_decode(file_get_contents("php://input"));
$query = "INSERT INTO classes (class_name, description, semester) VALUES (:class_name, :description, :semester)";
$stmt = $db->prepare($query);
$stmt->bindParam(':class_name', $data->class_name);
$stmt->bindParam(':description', $data->description);
$stmt->bindParam(':semester', $data->semester);
if($stmt->execute()) {
http_response_code(201);
echo json_encode(array("message" => "Class was created."));
} else {
http_response_code(503);
echo json_encode(array("message" => "Unable to create class."));
}
} elseif ($_SERVER['REQUEST_METHOD'] == 'GET') {
$query = "SELECT * FROM classes";
$stmt = $db->prepare($query);
$stmt->execute();
$classes = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($classes);
}
?>
2024/8/1 13:32:45
好的,让我们在你的`classes.php`文件中添加修改(PUT)和删除(DELETE)的逻辑。以下是包含GET、POST、PUT和DELETE请求处理逻辑的完整代码:
```php
require_once 'init.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 创建班级
$data = json_decode(file_get_contents("php://input"));
$query = "INSERT INTO classes (class_name, description, semester) VALUES (:class_name, :description, :semester)";
$stmt = $db->prepare($query);
$stmt->bindParam(':class_name', $data->class_name);
$stmt->bindParam(':description', $data->description);
$stmt->bindParam(':semester', $data->semester);
if ($stmt->execute()) {
http_response_code(201);
echo json_encode(array("message" => "Class was created."));
} else {
http_response_code(503);
echo json_encode(array("message" => "Unable to create class."));
}
} elseif ($_SERVER['REQUEST_METHOD'] == 'GET') {
// 获取班级列表
$query = "SELECT * FROM classes";
$stmt = $db->prepare($query);
$stmt->execute();
$classes = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($classes);
} elseif ($_SERVER['REQUEST_METHOD'] == 'PUT') {
// 修改班级信息
$data = json_decode(file_get_contents("php://input"));
$query = "UPDATE classes SET class_name = :class_name, description = :description, semester = :semester WHERE class_id = :class_id";
$stmt = $db->prepare($query);
$stmt->bindParam(':class_name', $data->class_name);
$stmt->bindParam(':description', $data->description);
$stmt->bindParam(':semester', $data->semester);
$stmt->bindParam(':class_id', $data->class_id);
if ($stmt->execute()) {
http_response_code(200);
echo json_encode(array("message" => "Class was updated."));
} else {
http_response_code(503);
echo json_encode(array("message" => "Unable to update class."));
}
} elseif ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
// 删除班级
$data = json_decode(file_get_contents("php://input"));
$query = "DELETE FROM classes WHERE class_id = :class_id";
$stmt = $db->prepare($query);
$stmt->bindParam(':class_id', $data->class_id);
if ($stmt->execute()) {
http_response_code(200);
echo json_encode(array("message" => "Class was deleted."));
} else {
http_response_code(503);
echo json_encode(array("message" => "Unable to delete class."));
}
} else {
// 处理不允许的请求方法
http_response_code(405);
echo json_encode(array("message" => "Method not allowed."));
}
```
解释新增的部分:
1. **PUT请求处理逻辑**:
- 首先,通过`file_get_contents("php://input")`读取客户端发送的JSON数据,并将其解码。
- 然后,准备一个SQL UPDATE语句,更新指定`class_id`的班级信息。
- 绑定新数据的参数和班级ID参数,并执行语句。如果更新成功,返回HTTP 200状态码和成功消息。如果更新失败,返回HTTP 503状态码和错误消息。
2. **DELETE请求处理逻辑**:
- 读取客户端发送的JSON数据,并将其解码。
- 准备一个SQL DELETE语句,删除指定`class_id`的班级。
- 绑定班级ID参数并执行语句。如果删除成功,返回HTTP 200状态码和成功消息。如果删除失败,返回HTTP 503状态码和错误消息。
确保你的前端代码在更新和删除班级时发送PUT和DELETE请求,并在请求体中包含所需的数据(如班级ID和更新后的新数据)。这样你的后端就可以正确处理这些请求。