我已经尝试过代码但不起作用。 select 语句工作正常,但 UPDATE 和 INSERT 语句没有对数据库进行任何更改。 即使进行的 DELETE 查询也不起作用。还有一个包含代码的 javascript 文件,然后连接到下面的该文件。
<?php
include("db_connect.php");
// handle a GET
// add the header line to specify that the content type is JSON
header("Content-type: application/json");
// determine the request type
$verb = $_SERVER["REQUEST_METHOD"];
if ($verb == "GET") {
$arr = array();
$rs = mysqli_query($link,"SELECT s.id, s.name, s.local_church_id as l_id, l.name as local_church_name, l.id as local_church_id from small_christian_community s, local_church l WHERE s.local_church_id = l.id ORDER BY l.name ASC");
while($obj = mysqli_fetch_object($rs)) {
$arr[] = $obj;
}
// add the header line to specify that the content type is JSON
header("Content-type: application/json");
echo "{\"data\":" .json_encode($arr). "}";
}
if ($verb == "PUT") {
$request_vars = Array();
parse_str(file_get_contents('php://input'), $request_vars );
$name = mysql_real_escape_string($request_vars["name"]);
$local_church_id = mysql_real_escape_string($request_vars["local_church_id"]);
// INSERT COMMAND
$insert_query = "INSERT INTO small_christian_community(name, local_church_id) VALUES ('".$name."','".$local_church_id."')";
$rs = mysqli_query($link,$insert_query);
if ($rs) {
echo json_encode($rs);
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo false;
}
}
if ($verb == "POST") {
$name = mysql_real_escape_string($_POST["name"]);
$local_church_id = mysql_real_escape_string($_POST["local_church_id"]);
$id = mysql_real_escape_string($_POST["id"]);
$rs = mysqli_query($link,"UPDATE small_christian_community SET name = '" .$name ."',local_church_id = '" .$local_church_id ."' WHERE id = " .$id);
if ($rs) {
echo json_encode($rs);
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo "Update failed for church: " .$id;
}
}
if ($verb == "DELETE") {
parse_str(file_get_contents('php://input'), $_DELETE);
$id = mysql_real_escape_string($_DELETE["id"]);
$rs = mysqli_query($link,"DELETE FROM small_christian_community WHERE id = " .$id);
if ($rs) {
echo true;
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo false;
}
}
?>
请有人帮忙
<?php
include("db_connect.php");
// Specify that the content type is JSON once
header("Content-type: application/json");
// Determine the request type
$verb = $_SERVER["REQUEST_METHOD"];
if ($verb == "GET") {
// Fetch all communities and local church information
$arr = array();
$query = "SELECT s.id, s.name, s.local_church_id as l_id,
l.name as local_church_name, l.id as local_church_id
FROM small_christian_community s
JOIN local_church l
ON s.local_church_id = l.id
ORDER BY l.name ASC";
$rs = mysqli_query($link, $query);
if ($rs) {
while ($obj = mysqli_fetch_object($rs)) {
$arr[] = $obj;
}
// Output result as JSON
echo json_encode(["data" => $arr]);
} else {
header("HTTP/1.1 500 Internal Server Error");
echo json_encode(["error" => mysqli_error($link)]);
}
}
if ($verb == "PUT") {
// Parse incoming PUT data
parse_str(file_get_contents('php://input'), $request_vars);
$name = mysqli_real_escape_string($link, $request_vars["name"]);
$local_church_id = mysqli_real_escape_string($link, $request_vars["local_church_id"]);
// INSERT query
$insert_query = "INSERT INTO small_christian_community (name, local_church_id)
VALUES ('$name', '$local_church_id')";
$rs = mysqli_query($link, $insert_query);
if ($rs) {
echo json_encode(["success" => true]);
} else {
header("HTTP/1.1 500 Internal Server Error");
echo json_encode(["error" => mysqli_error($link)]);
}
}
if ($verb == "POST") {
// Handle update operation
$name = mysqli_real_escape_string($link, $_POST["name"]);
$local_church_id = mysqli_real_escape_string($link, $_POST["local_church_id"]);
$id = mysqli_real_escape_string($link, $_POST["id"]);
// UPDATE query
$update_query = "UPDATE small_christian_community
SET name = '$name', local_church_id = '$local_church_id'
WHERE id = $id";
$rs = mysqli_query($link, $update_query);
if ($rs) {
echo json_encode(["success" => true]);
} else {
header("HTTP/1.1 500 Internal Server Error");
echo json_encode(["error" => "Update failed for church ID: $id", "details" => mysqli_error($link)]);
}
}
if ($verb == "DELETE") {
// Parse incoming DELETE data
parse_str(file_get_contents('php://input'), $_DELETE);
$id = mysqli_real_escape_string($link, $_DELETE["id"]);
// DELETE query
$delete_query = "DELETE FROM small_christian_community WHERE id = $id";
$rs = mysqli_query($link, $delete_query);
if ($rs) {
echo json_encode(["success" => true]);
} else {
header("HTTP/1.1 500 Internal Server Error");
echo json_encode(["error" => "Delete failed for church ID: $id", "details" => mysqli_error($link)]);
}
}
?>