我有一个调用数据库表来提取记录的 html 文件,以及一个更新对数据库表的更改的 php 文件,并将用户引导回 html 文件(以重新加载更新的记录)。
html 文件加载用户数据,获取输入,将其发布到 php 文件,php 文件将其写入 DB 表,然后重新加载 html 文件。但是,重新加载时,它会显示旧的记录信息,而不是新更新的信息。我查看数据库表,更新运行正确,我看到了新信息。但它不会反映在页面上。我该如何解决?
有一个更好的方法吗?谢谢。
HTML 文件:
<?php
session_start();
$_SESSION['email'] = $email;
if(isset($_SESSION['email'])) {
echo $_SESSION['email'], " is logged in."; // echoes 1
}
else {
echo "User not logged in";
}
?>
<?php
$query = "SELECT email, username, address1, city, state, postalcode, country
FROM users
WHERE email = '$_SESSION[email]';";
$result = mysql_query($query);
if ($result) {
$_SESSION['userdata'] = mysql_fetch_array($result, MYSQL_ASSOC);
}
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<body>
<form name="userprofile" action="update.php" method="post">
<fieldset>
<legend>Update User Profile</legend>
<br class="clear" />
<label for="username">Username</label>
<input type='text' name='username' value='<?php echo $_SESSION['userdata']['username']; ?>'/>
<br class="clear" />
<label for="address1">Address</label>
<input type='text' name='address1' value='<?php echo $_SESSION['userdata']['address1']; ?>'/><br>
<br class="clear" />
<label for="city">City</label>
<input type='text' name='city' value='<?php echo $_SESSION['userdata']['city']; ?>'/><br>
<br class="clear" />
<label for="state">State</label>
<input type='text' name='state' value='<?php echo $_SESSION['userdata']['state']; ?>'/><br>
<br class="clear" />
<label for="postalcode">Postal code</label>
<input type='text' name='postalcode' value='<?php echo $_SESSION['userdata']['postalcode']; ?>'/><br>
<br class="clear" />
<label for="country">Country</label>
<input type='text' name='country' value='<?php echo $_SESSION['userdata']['country']; ?>'/><br>
<br class="clear" />
<br class="clear" />
</fieldset>
<input type="submit" value="Update" />
</form>
</body>
</html>
<br class="clear" />
</form>
</html>
PHP 文件:
<?php
session_start();
$_SESSION['email'] = $email;
if(isset($_SESSION['email'])) {
echo $_SESSION['email'], " is logged in."; // echoes 1
}
else {
echo "User not logged in";
}
$username = $_POST['username'];
$address1 = $_POST['address1'];
$city = $_POST['city'];
$state = $_POST['state'];
$postalcode = $_POST['postalcode'];
$country = $_POST['country'];
$dbhost = 'localhost';
$dbname = 'dbtable';
$dbuser = 'dbuser';
$dbpass = 'password'; //not really
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);
$query = "UPDATE users
SET username ='$username',
address1 = '$address1',
city = '$city',
state = '$state',
postalcode = '$postalcode',
country = '$country'
WHERE email = '$_SESSION[email]';";
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
//echo $query;
mysql_query($query);
$retval = mysql_query($query);
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close();
header('Location: update.html');
?>