POST 和 GET 在 Postman 中有效,但 PUT 和 DELETE 无效
POST and GET works in Postmen but PUT and DELETE doesn't
我正在尝试一个简单的 Rest api GET 和 POST 方法有效但是当我尝试测试 PUT 和 DELETE Undefined Index
错误显示并且 ofc 数据库没有没有更新,我尝试通过 form-data
和 raw
ass 以及 x-www-form-urlencoded
输入数据,但是什么都没有
当我手动将数据输入我的代码而不是 $_REQUEST
时,它工作得很好
这是我的代码:
<?php
try{
include 'connection.php';
$method_name=$_SERVER["REQUEST_METHOD"];
if($_SERVER["REQUEST_METHOD"])
{
switch ($method_name)
{
case 'GET':
$qry="SELECT * from product";
$result=mysqli_query($conn, $qry);
while ($row=mysqli_fetch_row($result))
{
$temp_cat[]=array("product_id"=>$row[0],"product_name"=>$row[1],"product_price"=>$row[2],"product_qty"=>$row[3]);
}
$data=array("status"=>"1","message"=>"success","result"=>$temp_cat);
break;
case 'POST':
$name=$_REQUEST['product_name'];
$price=$_REQUEST['product_price'];
$qty=$_REQUEST['product_qty'];
$qry="INSERT INTO product(product_name,product_price,product_qty) values('$name','$price','$qty')";
if(mysqli_query($conn, $qry))
{
$data=array("status"=>"1","message"=>"success","result"=>"Product add successfully");
}
else{
$data=array("status"=>"1","message"=>"success","result"=>"Something wrong!!!");
}
break;
case 'PUT':
$id=$_REQUEST['product_id'];
$name=$_REQUEST['product_name'];
$price=$_REQUEST['product_price'];
$qty=$_REQUEST['product_qty'];
$qry="UPDATE product SET product_name='".$name."', product_price='".$price."',product_qty='".$qty."' where product_id='".$id."' ";
if(mysqli_query($conn, $qry))
{
$data=array("status"=>"1","message"=>"success","result"=>"Product Update successfully");
}
else{
$data=array("status"=>"1","message"=>"success","result"=>"Something wrong!!!");
}
break;
case 'DELETE':
$id=$_REQUEST['product_id'];
$qry="delete from product where product_id='".$id."'";
if(mysqli_query($conn, $qry))
{
$data=array("status"=>"1","message"=>"success","result"=>"Product Update successfully");
}
else{
$data=array("status"=>"1","message"=>"success","result"=>"Something wrong!!!");
}
break;
}
echo json_encode($data);
}
else{
$data=array("status"=>"0","message"=>"Please enter proper request method !! ");
echo json_encode($data);
}
}
catch(Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
this is a screenshot of postman while trying DELETE
请记住,我是新手,这实际上是我第一次使用它!
在PUT和DELETE的情况下你需要这样获取请求参数:
$requestParams = array();
parse_str(file_get_contents('php://input'), $requestParams);
然后您可以使用 $_GET 或 $_POST 访问这些值:
$requestParams['product_id']
希望对您有所帮助。
我正在尝试一个简单的 Rest api GET 和 POST 方法有效但是当我尝试测试 PUT 和 DELETE Undefined Index
错误显示并且 ofc 数据库没有没有更新,我尝试通过 form-data
和 raw
ass 以及 x-www-form-urlencoded
输入数据,但是什么都没有
当我手动将数据输入我的代码而不是 $_REQUEST
时,它工作得很好
这是我的代码:
<?php
try{
include 'connection.php';
$method_name=$_SERVER["REQUEST_METHOD"];
if($_SERVER["REQUEST_METHOD"])
{
switch ($method_name)
{
case 'GET':
$qry="SELECT * from product";
$result=mysqli_query($conn, $qry);
while ($row=mysqli_fetch_row($result))
{
$temp_cat[]=array("product_id"=>$row[0],"product_name"=>$row[1],"product_price"=>$row[2],"product_qty"=>$row[3]);
}
$data=array("status"=>"1","message"=>"success","result"=>$temp_cat);
break;
case 'POST':
$name=$_REQUEST['product_name'];
$price=$_REQUEST['product_price'];
$qty=$_REQUEST['product_qty'];
$qry="INSERT INTO product(product_name,product_price,product_qty) values('$name','$price','$qty')";
if(mysqli_query($conn, $qry))
{
$data=array("status"=>"1","message"=>"success","result"=>"Product add successfully");
}
else{
$data=array("status"=>"1","message"=>"success","result"=>"Something wrong!!!");
}
break;
case 'PUT':
$id=$_REQUEST['product_id'];
$name=$_REQUEST['product_name'];
$price=$_REQUEST['product_price'];
$qty=$_REQUEST['product_qty'];
$qry="UPDATE product SET product_name='".$name."', product_price='".$price."',product_qty='".$qty."' where product_id='".$id."' ";
if(mysqli_query($conn, $qry))
{
$data=array("status"=>"1","message"=>"success","result"=>"Product Update successfully");
}
else{
$data=array("status"=>"1","message"=>"success","result"=>"Something wrong!!!");
}
break;
case 'DELETE':
$id=$_REQUEST['product_id'];
$qry="delete from product where product_id='".$id."'";
if(mysqli_query($conn, $qry))
{
$data=array("status"=>"1","message"=>"success","result"=>"Product Update successfully");
}
else{
$data=array("status"=>"1","message"=>"success","result"=>"Something wrong!!!");
}
break;
}
echo json_encode($data);
}
else{
$data=array("status"=>"0","message"=>"Please enter proper request method !! ");
echo json_encode($data);
}
}
catch(Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
this is a screenshot of postman while trying DELETE
请记住,我是新手,这实际上是我第一次使用它!
在PUT和DELETE的情况下你需要这样获取请求参数:
$requestParams = array();
parse_str(file_get_contents('php://input'), $requestParams);
然后您可以使用 $_GET 或 $_POST 访问这些值:
$requestParams['product_id']
希望对您有所帮助。