gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:47 25 4
gpt4 key购买 nike

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

奇怪,提交后回显“保存成功!”正常显示,但不更新。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com