首页 > 解决方案 > 如何通过 PHP 中的 URL 更新数据库中的数据

问题描述

我是网络开发的新手。我想从这样的 URL 更新我的数据库的数据:谁能告诉我我的代码有什么问题?因为我从过去 2 天开始尝试但无法找到解决方案。希望得到社区的解答。提前致谢!!

http://localhost/repos/index.php?id=1&status=approved

索引.php

<?php

    $conn=mysqli_connect("localhost","root","root","angdb");

    $request=$_SERVER['REQUEST_METHOD'];

    $data=array();
    switch($request)
    {
        case 'GET':
            response(getData());
            break;

        case 'PUT':
            response(updateData());

        default:
            #code...
            break;
    }

    function getData()
    {
        global $conn;

        if(@$_GET['id'])
        {
            @$id=$_GET['id'];

            $where="AND id=".$id;
        }
        else
        {
            $id=0;
            $where="";
        }


        $query=mysqli_query($conn,"select * from vendor where status='pending' ".$where);
        while($row=mysqli_fetch_assoc($query))
        {
            $data[]=array("id"=>$row['id'],"changeColumn"=>$row['changeColumn'],"type"=>$row['type'],"timestamp"=>$row['timestamp'],"status"=>$row['status'],"name"=>$row['name']);
        }
        return $data;
    }

    function updateData()
    {
        global $conn;
        parse_str(file_get_contents('php://input'),$_PUT);

        if(@$_GET['id'])
        {
            @$id=$_GET['id'];

            $where="where id=".$id;
        }
        else
        {
            $id=0;
            $where="";
        }

        $query=mysqli_query($conn,"update vendor set status='".$_PUT['status']."'".$where);


        if($query==true)
        {
            $data[]=array("Message"=>"Updated");
        }
        else
        {
            $data[]=array("Message"=>"Not updated");
        }
        return $data;
    }

    function response($data)
    {
        echo json_encode($data);
    }
?>

标签: php

解决方案


状态在查询参数中,而不是在发布的数据中。所以$_PUT['status']应该是$_GET['status']


推荐阅读