您好,我有以下代码,它显示数据库中的数据,但不将数据添加到数据库中

问题描述 投票:0回答:1

我已经尝试过代码但不起作用。 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 mysql kendo-ui
1个回答
0
投票
<?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)]);
    }
}
?>
  1. 使用 mysqli 时将 mysql_real_escape_string 替换为 mysqli_real_escape_string。
  2. 合并JSON头声明以避免重复。
  3. 在 SQL 查询中使用 JOIN 语法以提高可读性并提高性能。
  4. 使用 mysqli_error() 添加了更强大的错误处理,以在失败时捕获并报告 SQL 错误。
  5. 使 JSON 响应保持一致,并添加更多信息性错误消息。
  6. 删除了多余的行以提高可读性和可维护性。
© www.soinside.com 2019 - 2024. All rights reserved.