当POST表单从previuos GET表单获取ID时,无法更新表

时间:2016-01-06 08:59:41

标签: mysql forms

在我的index.php页面上,我有一个表单method='GET' action='new.php',它从mySQL查询中获取它的值。

在new.php页面的顶部:$id = $_GET['id'],它从index.php页面中的previuos表单中检索id。在new.php页面上,我还有一个表单,在$id的帮助下通过查询得到它的值,但这次是POST表单,用mySQL查询更新表。

但表格没有更新。这是为什么?我知道我的所有代码都正确,因为当我将index.php页面上的GET更改为POST时,表格正在更新。

编辑:这是来自new.php的代码

require_once('include/connect.php');

 $id  = htmlentities ($_GET['id']);

if (isset($_POST['edit_client'])) {
    if ($_POST['edit_client'] == "1") {
        $_POST['edit_client'] = "0";

        $query3 = "SELECT * FROM client WHERE id='" . $id ."'";
        $result3 = $connect->query($query3);

        $result_rs3 = mysqli_fetch_assoc($result3);

        if(mysqli_num_rows($result3)!=0){
            do{
                echo"<form action='' method='POST' class='ajaxform'>
                    <div class='col-md-6 padding0 kundForm'>
                        <div class='controls controls-row left'>
                            <label>Firstname</label>
                            <input type='text' size='22' class='form-control' name='firstname' value='" . $result_rs3['firstname'] . "'>
                         </div>

                         <div class='controls controls-row left marginLeft10'>
                            <label>Lastname</label>
                            <input type='text' size='22' class='form-control span3' name='lastname' value='" . $result_rs3['lastname'] . "'>
                        </div>

                    </div>
                    <div class='marginTop20 clear'>
                        <input type='submit' value='Save' class='btn btn-primary btn-danger center-block' onclick=\"return confirm('Save changes?')\"/> 
                        <input type='hidden' name='edit_name' value='1'/>
                        <input type='hidden' name='id' value='" . $id . "'/>
                    </div>

                </form> \n";


            }
                while($result_rs3 = mysqli_fetch_assoc($result3));  
        }
    }

}else if (isset($_POST['edit_name'])) {
    if ($_POST['edit_name'] == "1") {
        $_POST['edit_name'] = "0";

        $id         = htmlentities ($_POST['id']);
        $firstname  = htmlspecialchars ($_POST['firstname']);
        $lastname   = htmlspecialchars ($_POST['lastname']);


        $query2 =  "UPDATE client SET 
                    firstname         ='" . $firstname .  "', 
                    lastname       ='" . $lastname . "', 
                    WHERE id        ='" . $id . "'";

        $connect->query($query2);
        mysqli_close($connect);  

    }
}

1 个答案:

答案 0 :(得分:0)

自己解决了。搞砸了我的if-statementment。将else if{}更改为if{},这就是诀窍!