使用php使用query更新数据库中的配置文件细节

fiei3ece  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(433)

我似乎无法更新数据库中的配置文件详细信息。它没有任何错误,只是没有显示在数据库中。在整个登录过程中,我使用session帮助存储用户名。这是我的updateprof.php

<?php
   session_start();
    ob_start();
    If (!isset($_SESSION["username"])){
        $_SESSION['username'] = $username;
            header("Location:logreg.php");
        }
    else {
    $username = $_SESSION['username'];
    }
 ?>    

<?php
   $username = $_SESSION['username'];
   $mysqli = new mysqli("localhost", "mp01_user", "venture@1998", "mp01_game");
  $stmt = $mysqli-> prepare("SELECT username, name, email, gender, dob FROM users WHERE username= ?");
        $stmt->bind_param("s", $username); 
        $stmt->execute();
        $stmt->bind_result($username, $name, $email, $gender, $dob);
        $stmt->fetch();

   echo "<table id='tableupprof' cellpadding='5' cellspacing='0' width='600' align='center' >";
   echo "<form action='profupdated.php?username=$username' method='post'>";
//Username
   echo "<tr><td>UserID:</td> <td><input type='text' name='username' value='". $username. "' style='width:380px; border:1px solid #999999' readonly></td></tr>";
//Name
   echo "<tr><td>Name:</td> <td><input type='text' name='name' value='". $name. "' style='width:380px; border:1px solid #999999' required></td></tr>";
//email
   echo "<tr><td>Email:</td><td><input type='text' name='email' value='". $email. "' style='width:380px; border:1px solid #999999' required> </td></tr><br><br>";
//gender
   echo "<tr><td>Gender: <td><input type ='text' name='gender' value='". $gender. "' style='width:380px; border:1px solid #999999;' required></td></tr>";
//dob
   echo "<tr><td>Date of Birth: <td><input type='text' name='dob' value='". $dob. "' style='width:380px; border:1px solid #999999;' readonly></td></tr>";

//Submit Form
   echo "<tr><td></td><td><input type='submit' name='submit' value='Update' />";
   echo "<input type='reset' value='Reset'  /></td></tr>";
   echo "</form></table>";

$stmt->execute();
$stmt->close();
$mysqli->close();

ob_end_flush()  
?>

一旦用户单击“update”,它将导致profupdated.php?username=\uuu(用户名),例如,profupdated.php?username=max
对于profupdated.php:

<?php
   session_start();
   ob_start();
      If (!isset($_SESSION["username"])){
        $_SESSION['username'] = $username;
            header("Location:logreg.php");
        }
   else {
   $username = $_SESSION['username'];
  }
 ?>

  <?php
        if( $_POST )
    {$connect = @mysql_connect("localhost", "mp01_user", "venture@1998");

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

     mysql_select_db("mp01_game", $connect);

     $username=$_POST['username'];
     $name=$_POST['name'];
     $email=$_POST['email'];
     $gender=$_POST['gender'];
     $dob=$_POST['dob'];

     $username = mysql_real_escape_string($username);
     $name = mysql_real_escape_string($name);
     $email = mysql_real_escape_string($email);
     $gender = mysql_real_escape_string($gender);
     $dob = mysql_real_escape_string($dob);

    $query = "UPDATE `users` SET `name` = $name, `email` = $email, `gender` 
   = $gender, `dob`=$dob WHERE `username` = $username";

  mysql_query($query);

   echo "<div id='h2'>You have update successfully!<br>";

        echo "<br><br> Please <a href='login.html'>Click Here</a> to login!";

    mysql_close($connect);
   } 

?>
4urapxun

4urapxun1#

你说没有错误-这可能是因为你没有试图捕捉错误。
你能替换吗

mysql_query($query);

echo "<div id='h2'>You have update successfully!<br>";

echo "<br><br> Please <a href='login.html'>Click Here</a> to login!";

mysql_close($connect);

具有

if(mysql_query($query) {
    /* If the query is successful, echo the success message and login link */
    echo "<div id='h2'>You have update successfully!<br>";
    echo "<br><br> Please <a href='login.html'>Click Here</a> to login!";
    mysql_close($connect);

} elseif (mysql_errno()){
    /* If the query is not successful - return the error */
    echo mysql_error($sql);
    mysql_close($connect);
}

这将捕获sql返回的所有错误。
另外-我看到你在mysqli和mysql之间切换-为了一致性,我建议你坚持使用其中一个。
除此之外-仅供参考,mysql扩展在php7中已被弃用,因此我建议您继续使用mysqli。

相关问题