I'm trying to update the record through a html form. But I have a problem that when I click SUBMIT then if ($ _SERVER ['REQUEST_METHOD'] == "PUT") {} is not activated.
My form 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>
And PHP code:
$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);
}
}
}