gpt4 book ai didi

PHP 页面在用户提交后存储表单输入变量

转载 作者:行者123 更新时间:2023-12-01 00:54:49 27 4
gpt4 key购买 nike

我不确定标题还能叫什么...我有一个访问特定 MySQL 数据库的 PHP 页面,从表中提取值,并将它们放在 HTML 表单中(POST 方法 - PHP_SELF)。然后用户可以查看这些值,根据需要更改它们并提交它们。该页面然后获取这些值并更新 MySQL 数据库。一切正常,除了当用户提交并且页面显示新的更新变量时,它仍然显示旧值。在新变量出现之前,用户被迫刷新页面。我认为 PHP 可能没有删除变量,所以我在脚本结束后取消设置所有存储的变量,但它仍然无法正常工作。我曾尝试在脚本开始之前设置一个 sleep 定时器,但也没有用。我将不胜感激任何建议。这是我的脚本,仅供引用:

<html>
<body>

<?php

$sql = "SELECT * FROM lease";
$result = mysql_query($sql);

?>

<form id="lease_update" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<table>
<tr>
<th>Account</th>
<th>Car Lease</th>
<th>Radio Lease</th>
<th>Misc. Charges</th>
</tr>

<?php
while($rows = mysql_fetch_array($result)){
?>

<tr>
<td><input type="text" name="account[]" value="<?php echo $rows['accnt']; ?>" /></td>
<td><input type="int" name="car_lease[]" value="<?php echo $rows['car']; ?>" /></td>
<td><input type="int" name="radio_lease[]" value="<?php echo $rows['radio']; ?>" /> </td>
<td><input type="int" name="misc_lease[]" value="<?php echo $rows['misc']; ?>" /></td>
<input type="hidden" name="lease_ID[]" value="<?php echo $rows['ID']; ?>" />
</tr>

<?php
}
?>
</table>
<input type="submit" value="Update" name="lease_update" />

<?php

if(isset($_POST['lease_update'])){

$account = $_POST['account'];
$car_lease = $_POST['car_lease'];
$radio_lease = $_POST['radio_lease'];
$misc_lease = $_POST['misc_lease'];
$lease_ID = $_POST['lease_ID'];

//Get Array Lengths For Each Section
$A = count($lease_ID);

//Update Lease Information
$i = 0;
while($i < $A){
if(!mysql_query('UPDATE lease SET accnt = "' .$account[$i]. '", car = "' .$car_lease[$i]. '", radio = "' .$radio_lease[$i]. '", misc = "' .$misc_lease[$i]. '" WHERE ID = ' .$lease_ID[$i]))
die('Error: ' .mysql_error());
$i++;
}
unset($_POST);
unset($rows);
unset(result);

}
?>
</body>
</html>

最佳答案

在更新数据库之前,您正在显示数据库中的数据。

在页面顶部进行所有数据库连接,然后显示结果通常是一种很好的做法。

在您的代码中(即使用户已提交更新),您查询数据,从数据库中提取并显示它,然后使用用户提交的内容运行更新。

将您的代码更改为此应该可以解决问题(尽管请阅读下面的注释):

<html>
<body>

<?php

if(isset($_POST['lease_update'])){

$account = $_POST['account'];
$car_lease = $_POST['car_lease'];
$radio_lease = $_POST['radio_lease'];
$misc_lease = $_POST['misc_lease'];
$lease_ID = $_POST['lease_ID'];

//Get Array Lengths For Each Section
$A = count($lease_ID);

//Update Lease Information
$i = 0;
while($i < $A){
if(!mysql_query('UPDATE lease SET accnt = "' .$account[$i]. '", car = "' .$car_lease[$i]. '", radio = "' .$radio_lease[$i]. '", misc = "' .$misc_lease[$i]. '" WHERE ID = ' .$lease_ID[$i]))
die('Error: ' .mysql_error());
$i++;
}
unset($_POST);
unset($rows);
unset(result);

}


$sql = "SELECT * FROM lease";
$result = mysql_query($sql);

?>

<form id="lease_update" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<table>
<tr>
<th>Account</th>
<th>Car Lease</th>
<th>Radio Lease</th>
<th>Misc. Charges</th>
</tr>

<?php
while($rows = mysql_fetch_array($result)){
?>

<tr>
<td><input type="text" name="account[]" value="<?php echo $rows['accnt']; ?>" /></td>
<td><input type="int" name="car_lease[]" value="<?php echo $rows['car']; ?>" /></td>
<td><input type="int" name="radio_lease[]" value="<?php echo $rows['radio']; ?>" /> </td>
<td><input type="int" name="misc_lease[]" value="<?php echo $rows['misc']; ?>" /></td>
<input type="hidden" name="lease_ID[]" value="<?php echo $rows['ID']; ?>" />
</tr>

<?php
}
?>
</table>
<input type="submit" value="Update" name="lease_update" />

</body>
</html>

错误提示 - 您的代码很容易受到注入(inject)攻击。您使用的表单数据未经过 验证。这是一个很大的危险信号。其次,您使用的是已弃用的 mysql_* 函数。你的代码应该使用 mysqli_* 函数或者更好的是移动到 PDO .它更安全,您可以用它做更多事情。

编辑 2:页面在用户提交表单后更新,但您向用户显示的页面在更新之前查询数据库 - 并使用它向用户显示页面。

关于PHP 页面在用户提交后存储表单输入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12035414/

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