php - 提交按钮 HTML/PHP 时更新数据库

标签 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!';
    }
}

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

最佳答案

当您提交表单时,页面会重新加载 $_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 注入(inject)攻击。我建议你使用参数化查询

$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>
';
}

关于php - 提交按钮 HTML/PHP 时更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54514332/

相关文章:

javascript - jQuery PHP 帖子

mysql - 我以某种方式设法在 MySQL 表中获得两个具有相同名称的索引,这是一个问题吗?

php查询输出另存为文本文件

php - 使用 php/javascript 提交表单的正确方法

php - 使用 WHERE x IN (1,2,3) 清理 MySQL 资源

html - SRCSET 的 W3C 验证

php - 在 Laravel 查询/子查询构建器中添加左连接

javascript - 如何使用 Cypress 检测 HTML 表单验证中 setCustomValidity 的输出

mysql - 在 when 子句中替换 MySQL 查询中的空值

php - 在选项卡上单击加载内容