首页 > 解决方案 > 尝试在 phpmyadmin 中动态传递记录 ID 时获取 http_response_code(404)。我正在尝试将 MySQL on-pre 与 Salesforce 集成

问题描述

我正在尝试更新与动态传递的 id 对应的特定记录或行。有一条属于该id的记录。当我给 URL 后跟“?id=3”时,我收到 404 错误,说不存在这样的记录。想知道我犯了什么错误。以下是我的代码。

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/database.php';
include_once '../objects/constitute.php';

$database = new Database();
$db = $database->getConnection();
$constitute = new Constitute($db);

$data = json_decode(file_get_contents("php://input"));
$constitute->id = isset($_GET['id']) ? $_GET['id'] : die();
$constitute->name = $data->name;
$constitute->address = $data->address;
$constitute->constitution_type = $data->constitution_type;
$constitute->organisation_id = $data->organisation_id;

if($constitute->update()){
    http_response_code(200);
    echo json_encode(array("message" => "Constitute was updated."));
}
 else{
    http_response_code(503);
    echo json_encode(array("message" => "Unable to update constitute."));
}
?>

以下是 update() 方法:

    function update(){
    $query = "UPDATE
                " . $this->table_name . "
            SET
                name = :name,
                address = :address,
                constitution_type = :constitution_type,
                organisation_id = :organisation_id
            WHERE
                id = ?";
    $stmt = $this->conn->prepare($query);

    $this->id=htmlspecialchars(strip_tags($this->id));
    $this->name=htmlspecialchars(strip_tags($this->name));
    $this->address=htmlspecialchars(strip_tags($this->address));
    $this->constitution_type=htmlspecialchars(strip_tags($this->constitution_type));
    $this->organisation_id=htmlspecialchars(strip_tags($this->organisation_id));

    $stmt->bindParam(':id', $this->id);
    $stmt->bindParam(':name', $this->name);
    $stmt->bindParam(':address', $this->address);
    $stmt->bindParam(':constitution_type', $this->constitution_type);
    $stmt->bindParam(':organisation_id', $this->organisation_id);

    if($stmt->execute()){
        return true;
    }
    return false;
}

另外,当我尝试在邮递员中传递更新的记录时,我得到了成功代码。但是记录没有得到保存。请让我知道我在哪里做错了。

标签: phpmysqljsonrestphpmyadmin

解决方案


推荐阅读