我正在尝试通过html表单更新记录。但是我有一个问题,当我单击“提交”时,如果($ _SERVER ['REQUEST_METHOD'] ==“ PUT”){}未激活。
我的表单html:
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
ID : <input type="text" name ="idCar1">
NAME CAR: <input type="text" name="nameCar1" >
YEAR CAR: <input type="text" name="yearCar1">
<input type="hidden" name='_METHOD' value="PUT">
<input type="submit" value="SUBMIT">
<br><br>
<span style="color:red">* <?php echo $idUpdateErr;?> </span>
</form>
和PHP代码:
$idUpdate="";
$idUpdateErr="";
if ($_SERVER['REQUEST_METHOD'] == "PUT"){
echo "debug";
if(empty($_POST["idCar"])){
$idUpdateErr="ID Car is required";
}
else{
$idUpdate=test_input($_POST["idCar"]);
$sql = " UPDATE cars
SET name = '".$nameCar."', year= '".$yearCar."'
WHERE id= $idUpdate" ;
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
header("Refresh:0");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
答案 0 :(得分:0)
在HTML5中,您无法在表单中设置方法PUT。
您可以做的是在表单内添加一个隐藏字段
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
ID : <input type="text" name ="idCar1">
NAME CAR: <input type="text" name="nameCar1" >
YEAR CAR: <input type="text" name="yearCar1">
<input type="hidden" name='_METHOD' value="PUT">
<input type="submit" value="SUBMIT">
<input type="hidden" name="_method" value="PUT">
<br><br>
<span style="color:red">* <?php echo $idUpdateErr;?> </span>
</form>