使用PHP在html中形成表单方法PUT

时间:2018-11-15 03:57:19

标签: php forms put

我正在尝试通过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);
            }
        }

   }
 请帮我 !!

1 个答案:

答案 0 :(得分:0)

在HTML5中,您无法在表单中设置方法PUT。

https://softwareengineering.stackexchange.com/questions/114156/why-are-there-are-no-put-and-delete-methods-on-html-forms

您可以做的是在表单内添加一个隐藏字段

<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>