0

我有一个调用数据库表来提取记录的 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');

?>
4

2 回答 2

0

您可以在 index.html 页面中执行此操作。不需要 index.php

<?php
session_start();
$dbhost = 'localhost';
$dbname = 'test';
$dbuser = 'root';
$dbpass = ''; //not really
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);

$email='ripon';
$_SESSION['email'] = $email;
if(isset($_SESSION['email'])) {
    echo $_SESSION['email'], " is logged in."; // echoes 1
}
else {
    echo "User not logged in";
}
?>

<?php
if(isset($_POST['submit']))
{
    $username = $_POST['username'];
    $address1 = $_POST['address1'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $postalcode = $_POST['postalcode'];
    $country = $_POST['country'];
    echo $address1; 
    $dbhost = 'localhost';
    $dbname = 'test';
    $dbuser = 'root';
    $dbpass = ''; //not really
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_select_db($dbname, $conn);

    $query = "UPDATE user SET   password = '$address1' WHERE username = '$_SESSION[email]';";

    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }

    //echo $query;

    mysql_query($query) or die(mysql_error());
    $retval = mysql_query($query);

    if(! $retval )
    {
      die('Could not update data: ' . mysql_error());
    }
    echo "Updated data successfully\n";
    //mysql_close();
}


?>

<?php
$query = "SELECT  username, password, fullname FROM user WHERE username = '$_SESSION[email]';";
$result = mysql_query($query);
if ($result) {
    $_SESSION['userdata'] = mysql_fetch_array($result, MYSQL_ASSOC);
}
else
{
echo "error";
echo $query;
}
//mysql_close();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<body>
<?php var_dump($_SESSION['userdata']);?>
  <form name="userprofile" action="update.html" 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']['password']; ?>'/><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"  name="submit"/>
  </form>
</body>
</html>
<br class="clear" />

</form>
</html>
于 2013-11-09T09:44:13.450 回答
0

一旦数据更新到数据库表中,您需要将更新的数据重新分配到会话中。您可能需要像这样修改您的代码

    <?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;
$retval = mysql_query($query);

if($retval){
     $_SESSION['userdata']['username']=$username; 
     $_SESSION['userdata']['address1']=$address1; 
     $_SESSION['userdata']['city']=$city;
     $_SESSION['userdata']['state']=$state;
     $_SESSION['userdata']['postalcode']=$postalcode;
     $_SESSION['userdata']['country']=$country;
    echo "Updated data successfully\n";
    mysql_close();
    header('Location: update.html');
}else{
  die('Could not update data: ' . mysql_error());
}
?>
于 2013-11-09T09:30:04.743 回答