php - 如何通过 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);
}
?>
解决方案
状态在查询参数中,而不是在发布的数据中。所以$_PUT['status']
应该是$_GET['status']
推荐阅读
- java - 目标运行时 WebSphere Application Server 传统版 V9.0 未定义
- python - 获得最大收益的算法
- filesystems - 在 ubifs 卷上应用增量补丁
- html - 具有三种状态的复选框
- python - 如何仅在 tkinter 中更改所选文本的属性
- firebase - Firebase Cloud Functions:`convert` 因 ChildProcessError 而失败
- reactjs - 如何将 react-helmet 与 netlify 预渲染一起使用?
- python - 如何使用 atom-python-run 包在 Atom 中运行 python 脚本?
- git - 旧的提交出现在新的拉取请求中?
- java - 无法在线性布局中移动项目