gpt4 book ai didi

php - 使用 PHP 查询更新数据库中的个人资料详细信息

转载 作者:行者123 更新时间:2023-11-29 10:01:22 25 4
gpt4 key购买 nike

我似乎无法更新数据库中的个人资料详细信息。它没有任何错误,只是没有显示在数据库中。我使用 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()
?>

一旦用户点击“更新”,就会出现 profupdated.php?username=___ (用户名),例如 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);
}

?>

最佳答案

您说没有错误 - 这可能是因为您没有 try catch 错误。

可以换吗

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 之间切换 - 我建议您坚持使用其中之一以保持一致性。

除此之外 - 仅供引用,PHP7 中已弃用 MYSQL 扩展,因此我建议您坚持使用 MYSQLi。

关于php - 使用 PHP 查询更新数据库中的个人资料详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52870229/

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