当我提交按钮HTML / PHP时更新数据库

时间:2019-02-04 10:34:59

标签: php html mysql

我想创建一个表格,在其中我可以编辑并保存到数据库中以供以后在其他文件中使用的文本。 我编写了以下代码,数据库显示的文本很好,但是通过提交按钮更新文本无效。

这很奇怪,但是提交后回显“成功保存!”通常会显示,但不会更新。

$connection = new mysqli("localhost", "db", "password", "db");
$query = "SELECT text FROM mailtext";
$result = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($result))
{
    $text = iconv('iso-8859-2', 'utf-8', $row['text']);
    echo'
<center>
    <form id="mailtext" method="post">
        <textarea name="text" style="width: 500px; height: 300px;">'.$text.'</textarea>
        <input type="submit" name="submit" value="Save">
    </form>
</center>
';
    if(isset($_POST['submit'])) {
        $query2 = "UPDATE mailtext SET text='.$text.' WHERE id=1";
        mysqli_query($connection, $query2);
        echo 'Successfully saved!';
    }
}

如果您有任何疑问或类似的问题,请询问:) 是的,我尝试搜索其他问题的答案,但没有帮助。

2 个答案:

答案 0 :(得分:3)

提交表单时,页面将重新加载$_POST的内容,并从上到下重新执行。

//            Form is submitted
// You are getting the content of the table
//                  |
//                  |
//                  V
$connection = new mysqli("localhost", "db", "password", "db");
$query = "SELECT text FROM mailtext";
$result = mysqli_query($connection, $query);
//      You are displaying the form
//                  |
//                  |
//                  V
while($row = mysqli_fetch_assoc($result))
{
    $text = iconv('iso-8859-2', 'utf-8', $row['text']);
    echo'
<center>
    <form id="mailtext" method="post">
        <textarea name="text" style="width: 500px; height: 300px;">'.$text.'</textarea>
        <input type="submit" name="submit" value="Save">
    </form>
</center>
';
// You are treating the previously submitted form
//                  |
//                  |
//                  V
    if(isset($_POST['submit'])) {
        $query2 = "UPDATE mailtext SET text='.$text.' WHERE id=1";
        mysqli_query($connection, $query2);
        echo 'Successfully saved!';
    }
}

在查询数据库以获取信息之前,您必须处理以下形式: 。否则,数据将不会被插入/更新。

请注意,您的查询容易受到SQL注入的攻击。我建议您使用参数化查询

$connection = new mysqli("localhost", "db", "password", "db");
if(isset($_POST['submit'])) {
    $text = $_POST['text'];
    $query2 = "UPDATE mailtext SET text=? WHERE id=1";
    //                                  ^------------------- Set a parameter for the query
    $stmt = mysqli_prepare($connection, $query2);
    //      ^------------^---------------------------------- Prepare the query for parameters
    mysqli_stmt_bind_param($stmt, "s", $text);
//  ^--------------------^---------------------------------- bind the parameters
    mysqli_stmt_execute($stmt);
//  ^-----------------^------------------------------------- execute the safe query
    echo 'Successfully saved!';
}

$query = "SELECT text FROM mailtext";
$result = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($result))
{
    $text = iconv('iso-8859-2', 'utf-8', $row['text']);
    echo'
<center>
    <form id="mailtext" method="post">
        <textarea name="text" style="width: 500px; height: 300px;">'.$text.'</textarea>
        <input type="submit" name="submit" value="Save">
    </form>
</center>
';
}

答案 1 :(得分:0)

向表单添加空的动作属性 并更新您的代码

if(isset($_POST['submit'])) {
      $text= $_POST['text'];
    $query2 = "UPDATE mailtext SET text='.$text.' WHERE id=1";
    mysqli_query($connection, $query2);
    echo 'Successfully saved!';
}