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

use*_*781 0 html php mysql forms unset

我不知道还有什么可以称之为标题...我有一个访问某个MySQL数据库的PHP页面,从表中提取值,并将它们放在HTML表单中(POST方法 - PHP_SELF).然后,用户可以查看值,根据需要更改它们并提交它们.然后页面获取这些值并更新MySQL数据库.一切都很完美,除了当用户提交并且页面显示新的更新变量时,它仍然显示旧值.在新变量出现之前,用户被强制刷新页面.我认为PHP可能没有删除变量,所以我在脚本结束后取消设置所有存储的变量并且它仍然无法正常工作.我曾尝试在脚本启动之前安装睡眠定时器,但这也无效.我很感激任何建议.这是我的脚本仅供参考:

<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>
Run Code Online (Sandbox Code Playgroud)

Flu*_*feh 5

您在更新之前显示数据库中的数据.

通常,最佳做法是在页面顶部执行所有数据库连接,然后显示结果.

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

坏注意 - 您的代码对注入攻击持开放态度.您正在使用没有验证的表单数据.那是一个大红旗.其次,您正在使用已弃用的mysql_*函数.您的代码应该使用mysqli_*函数或更好,然后转移到PDO.它更安全,你可以用它做更多.

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