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

标签 php html mysql

我正在创建一个允许用户注册帐户并使用它登录的网站。
现在,我希望他们能够更新他们的详细信息,例如用户名和电子邮件。
我遵循了指南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/

相关文章:

php - 从 Symfony2 中的实体填充表单数据

html - 除非我尝试通过检查工具(检查元素)触摸该元素的 CSS,否则 Google Chrome 浏览器不会显示背景图像

php - 在php和namevalue中显示mysql的最小值和最大值?

html - 无法从 Bootstrap 菜单中删除黑条以显示我的菜单更改

php解析错误总是在最后一行

mysql - 如何使用可选子句左连接表

php - MySql Processlist 填充了 "Sleep"导致 "Too many Connections"的条目?

javascript - 使用 $.each jquery 在 ajax 成功中创建 html 选择选项

php - 在 macOS Sierra 上安装 PHP 7.4 而无需 brew?

php - SQL 查询 : order by length of characters?