gpt4 book ai didi

php & MySQL - 允许用户更新他们的详细信息

转载 作者:行者123 更新时间:2023-11-28 23:54:58 24 4
gpt4 key购买 nike

我正在创建一个允许用户注册帐户并使用它登录的网站。
现在,我希望他们能够更新他们的详细信息,例如用户名和电子邮件。
我遵循了指南here因为我碰巧在 register.php 中使用了与他相同的代码,但是它不起作用。
我是 php 的新手,所以请多多包涵!感谢您的帮助。

register.php

    <?php

if(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
}

$email = mysql_real_escape_string($_POST['email']);

$checkusername = mysql_query("SELECT * FROM users WHERE username = '".$username."'");

if(mysql_num_rows($checkusername) == 1)
{
echo "<h1>Error</h1>";
echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
}
else
{
$registerquery = mysql_query("INSERT INTO users (username, password, email) VALUES('".$username."', '".$password."', '".$email."')");
if($registerquery)
{
echo "<h1>Success</h1>";
echo "<p>Your account was successfully created. Please <a href=\"index.php\">click here to login</a>.</p>";
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your registration failed. Please go back and try again.</p>";
}
}

?>

<h1>Register</h1>

<p>Please enter your details below to register.</p>

<form method="post" action="register.php" name="registerform" id="registerform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<label for="email">Email Address:</label><input type="text" name="email" id="email" /><br />
<input type="submit" name="register" id="register" value="Register" />
</fieldset>
</form>

</div>
</body>
</html>

当我输入editprofile.php 时,页面是空白的。这是我的 editprofile.php

    <?php
include "base.php";
session_start();

if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['username']))
{

$nameuser = $_SESSION['username'];
$checkinfo = mysql_query("SELECT * FROM users WHERE username = '$nameuser'");

while($results = mysql_fetch_array($checkinfo,MYSQL_ASSOC)) {

$id = $results['id'];
$username = $results['username'];
$email = $results['email'];

}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$editNAME=$_POST['username'];
$editEMAIL=$_POST['email'];
$editID=$_POST['id'];

$editquery = mysql_query("UPDATE users SET username='$editNAME' , email='$editEMAIL' WHERE id='$editID'");

if($editquery)
{
echo "<b>Success!</b>";
echo "Your profile was successfully updated. Please click<a href=\"profile.php\"> here </a>to view.";
}
else
{
echo "<b>Error</b>";
echo "<p>Sorry, your profile update failed. Please go back and try again.</p>";
}
}
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Edit Profile</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<form method="post" action="editprofile.php" name="editform" id="editform">
<p>Username: <input type="text" name="username" id="username" value="<?php echo $username; ?>" /></p>
<p>Email Address : <input type="email" name="email" id="email" value="<?php echo $email; ?>"></p>
<input type="submit" name="save" value="Save"/>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
</form>
</body>

最佳答案

<?php
include "base.php";
session_start();
?>

<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['username']))
{

$nameuser = $_SESSION['username'];
$checkinfo = mysql_query("SELECT * FROM users WHERE username = '$nameuser'");

while($results = mysql_fetch_array($checkinfo,MYSQL_ASSOC)){

$id = $row['id'];
$username = $row['username'];
$email = $row['email'];
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$editNAME=$_POST['username'];
$editEMAIL=$_POST['email'];
$editID=$_POST['id'];

$editquery = mysql_query("UPDATE users SET username='$editNAME' , email='$editEMAIL' WHERE id='$editID'");

if($editquery)
{
echo "<b>Success!</b>";
echo "Your profile was successfully updated. Please click<a href=\"profile.php\"> here </a>to view.";
}
else
{
echo "<b>Error</b>";
echo "<p>Sorry, your profile update failed. Please go back and try again.</p>";
}
}
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Edit Profile</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<form method="post" action="editprofile.php" name="editform" id="editform">
<p>Username: <input type="text" name="username" id="username" value="<?php echo $username; ?>" /></p>
<p>Email Address : <input type="email" name="email" id="email" value="<?php echo $email; ?>"></p>
<input type="submit" name="save" value="Save"/>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
</form>
</body>

you try this.... Not 100% sure but you try..

关于php & MySQL - 允许用户更新他们的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31934612/

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