gpt4 book ai didi

php - 使用 PHP 将数据从 HTML 文本字段更新到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-30 23:29:06 25 4
gpt4 key购买 nike

我尝试使用 PHP 将数据从 HTML 文本字段更新到数据库。我想更新通知字段。我无法将文本字段值获取到 SQL 查询。这是我的代码:

$result = mysql_query($query) or die (mysql_error()); // Run query
while($row = mysql_fetch_array($result)) { ?>
<tr><td><?php echo $row[0];?></td>
<td><?php echo $row[1];?></td>
<td><?php echo $row[2];?></td>
<td><?php echo $row[3];?></td>
<td><?php echo $row[4];?></td>
<td><input type="text" name="notifications" value="<?php echo $row[5];?>" size=13></td></tr>
<?php
if (isset($_POST['save'])) {
for($i = 0; $i < $count; $i++) // For loop
{
$id=mysql_result($result,$i,"id"); // Get id from query
$notifications = $_POST['notifications'];
$update = "UPDATE testtable SET notifications = {$notifications[$i]} WHERE id = {$id}";
echo "$update<br>";
//mysql_query($update); // Run
}

最佳答案

看起来 notifications 字段可能是一个字符串,因此您需要使用引号将其转义,如下所示:

$update = "UPDATE testtable SET notifications = '{$notifications[$i]}' WHERE id = {$id}";

您的代码很容易受到注入(inject)攻击,您应该使用准备好的语句并转移到 PDO 或 msqli 来抵御潜在的攻击。<​​/p>

关于php - 使用 PHP 将数据从 HTML 文本字段更新到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11651562/

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