如何将html输入字段中的值保存到数据库中?

时间:2019-05-28 02:25:15

标签: php html

我知道这似乎是一个非常简单的问题,但这确实困扰了我一段时间。差不多,我在html中输入了一个东西,然后当用户单击一个按钮时,应该将他们在框中输入的信息存储到数据库中。

我已经知道将信息发送到数据库的部分可以工作了,因为我已经尝试过了。但这并不是在回响“作品!”因此它无法识别输入框中的任何输入。

$submit = @$_POST['test'];

$col= "";

$get_info = mysqli_query($connection, "SELECT col FROM users WHERE 
username = '$username'");
$get_row = mysqli_fetch_assoc($get_info);
$db_col = $get_row['col'];

$col =  @$_POST['coly'];

if($submit){

    if($col){

        echo"works!";
        $update_col = mysqli_query($connection, "UPDATE users SET col 
              = 
            '$col' WHERE username = '$username'");
    }
}

<div class = "profileLeftSideContent">

    <form action = "edit.php" method="post">

        <input type="text" name="coly" size="25" 
                 placeholder="School" 
             /> <br /><br />

    </form>
</div>

<form class="budon" method="POST">

<input  type="submit" name="test" id="test" value="Save"/><br/>

</form>

它应该将键入的信息发送到数据库,但是,它甚至没有回显“有效!”。因此它认为$ col没有输入任何内容。它没有给我任何错误,并且按钮可以正常工作。谢谢您的输入。

1 个答案:

答案 0 :(得分:0)

这不是因为$col一无所有,而是因为$submit为空,因为该表单没有名为'test'的字段,所以$_POST['test']不返回任何内容。

$submit = $_POST['test'];

$col= "";

$get_info = mysqli_query($connection, "SELECT col FROM users WHERE username = '$username'");
$get_row = mysqli_fetch_assoc($get_info);
$db_col = $get_row['col'];

$col =  $_POST['coly'];

if($submit){

    if($col){

        echo"works!";
        $update_col = mysqli_query($connection, "UPDATE users SET col = '$col' WHERE username = '$username'");
    }

}

<div class = "profileLeftSideContent">

    <form action = "edit.php" method="post">

        <input type="text" name="coly" size="25" placeholder="School"/> 
        <br /><br />
        <input type="submit" name="test" value="Submit"/>

    </form>

</div>