苦苦挣扎PHP更新用户

时间:2014-10-21 12:27:37

标签: php mysql

我刚刚创建了一个页面,用户可以在其中更改密码和电子邮件地址等详细信息。它有效,但现在我想允许用户更改他们的电子邮件而不必更改他们的密码。

所以基本上,如果用户只想更改他们的电子邮件,它将在数据库中更新,而不必更改他们的密码。

我该怎么做?

代码:

<title>Honda |</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href='http://fonts.googleapis.com/css?family=Julius+Sans+One' rel='stylesheet' type='text/css'>
<link href="../css/style.css" rel="stylesheet" type="text/css" media="all"/>

<?php
session_start();
$username = $_SESSION['sess_user'];

echo '<div class="search1"><h2>' . $username . '</h2></div>';

if (isset($_SESSION['sess_user'])) {
    //user is logged in

    if (isset($_POST['submit'])) {
        //start changing password
        //check fields

        $oldpassword = md5($_POST['oldpassword']);
        $newpassword = md5($_POST['newpassword']);
        $email = $_POST['email'];


        $repeatnewpassword = md5($_POST['repeatnewpassword']);


        //check password against db
        include('../includes/config.php');

        $queryget = mysql_query("SELECT password FROM login WHERE username='$username'") or die ("change password failed");
        $row = mysql_fetch_assoc($queryget);
        $oldpassworddb = $row['password'];

        //check passwords
        if ($oldpassword == $oldpassworddb) {
            //check two new passwords
            if ($newpassword == $repeatnewpassword) {
                //successs
                //change password in db
                $querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE   username='$username'");
                $querychange = mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");
                die("<div class='successmate'>Your password has been changed. <a href='index2.php'><br><br> Return</a></div>");
            } else
                die("<div class='results'>New password doesn't match!</div>");

        } else
            die("<div class='results'>Old password doesn't match!</div>");

    } else {

        echo "
<form class='search1' action='changepassword.php' method='POST'>
<label>Current Password:</label> <input type='password' id='password' name='oldpassword'><p>
<label>New Password:</label> <input type='password' id='password' name='newpassword'><p>
<label>Repeat New Password:</label> <input type='password'  name='repeatnewpassword'><p>
<label>Email:</label> <input type='email'  name='email'><p>
<input type='submit' name='submit' class='submit' value='submit'><br><br><br>
<h2><p><a href='index2.php'>Back</a></p></h2>
</form>
";
    }

} else
    die ("You must be logged in to change your password");
?>

<img src="../images/main.jpg">

任何帮助将不胜感激!

EDIT - 迈克尔:

<title>Honda |</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href='http://fonts.googleapis.com/css?family=Julius+Sans+One' rel='stylesheet' type='text/css'>
<link href="../css/style.css" rel="stylesheet" type="text/css" media="all" />



<?php
session_start();



$username = $_SESSION['sess_user'];

echo '<div class="search1"><h2>'.$username.'</h2></div>';


if (isset($_SESSION['sess_user']))
{
//user is logged in

if (isset($_POST['submit']))
{
//start changing password
//check fields

$oldpassword = md5($_POST['oldpassword']);
$newpassword = md5($_POST['newpassword']);
$email = $_POST['email'];


$repeatnewpassword = md5($_POST['repeatnewpassword']);


//check password against db
include('../includes/config.php');

$queryget = mysql_query("SELECT password FROM login WHERE username='$username'") or die ("change password failed");
$row = mysql_fetch_assoc($queryget);
$oldpassworddb = $row['password'];

//check passwords
if ($oldpassword==$oldpassworddb)
{
//check two new passwords
if ($newpassword==$repeatnewpassword)
{
   //successs
    //change password in db
    if (isset($_POST['repeatnewpassword']) AND isset($_POST['newpassword']) AND $_POST['newpassword'] != '') {
  if ($newpassword==$repeatnewpassword)
    {
    $querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE   username='$username'");
}}
  if (isset($_POST['email']) AND $_POST['email'] != '') {
    $querychange = mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");
    die("<div class='successmate'>Your password has been changed. <a href='index2.php'><br><br> Return</a></div>");
    }
else 
die("<div class='results'>New password doesn't match!</div>");

}else 
die("<div class='results'>Old password doesn't match!</div>");

}
else
{

echo"
<form class='search1' action='changepassword.php' method='POST'>
<label>Current Password:</label> <input type='password' id='password' name='oldpassword'><p>
<label>New Password:</label> <input type='password' id='password' name='newpassword'><p>
<label>Repeat New Password:</label> <input type='password'  name='repeatnewpassword'><p>
<label>Email:</label> <input type='email'  name='email'><p>
<input type='submit' name='submit' class='submit' value='submit'><br><br><br>
<h2><p><a href='index2.php'>Back</a></p></h2>
</form>
";
 }

 }else 
die ("You must be logged in to change your password");


?>

<img src="../images/main.jpg">

2 个答案:

答案 0 :(得分:0)

检查他们是否有兴趣重置除电子邮件以外的任何其他内容:

if (isset($_POST['submit'])) {
    //start changing password
    //check fields

    if(isset($_POST['oldpassword']) && (! empty($_POST['oldpassword']))){
          //password was posted, they may want to change it
            $oldpassword = md5($_POST['oldpassword']);
            $newpassword = md5($_POST['newpassword']);
     }
     ...

答案 1 :(得分:0)

您可以按如下方式更改代码:

    //successs
    //change password in db
    if (isset($_POST['repeatnewpassword']) AND isset($_POST['newpassword']) AND $_POST['newpassword'] != '') {
  if ($newpassword==$repeatnewpassword)
    {
    $querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE   username='$username'");
}}
  if (isset($_POST['email']) AND $_POST['email'] != '') {
    $querychange = mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");
    die("<div class='successmate'>Your password has been changed. <a href='index2.php'><br><br> Return</a></div>");
    }

编辑

完整页面:     

    $username = $_SESSION['sess_user'];

    echo '<div class="search1"><h2>'.$username.'</h2></div>';


    if (isset($_SESSION['sess_user']))
    {
        //user is logged in

        if (isset($_POST['submit']))
        {
            //start changing password
            //check fields

            $oldpassword = md5($_POST['oldpassword']);
            $newpassword = md5($_POST['newpassword']);
            $email = $_POST['email'];


            $repeatnewpassword = md5($_POST['repeatnewpassword']);


            //check password against db
            include('../includes/config.php');

            $queryget = mysql_query("SELECT password FROM login WHERE username='$username'") or die ("change password failed");
            $row = mysql_fetch_assoc($queryget);
            $oldpassworddb = $row['password'];

            //check passwords
            if ($oldpassword==$oldpassworddb)
            {

                if (isset($_POST['repeatnewpassword']) AND isset($_POST['newpassword']) AND $_POST['newpassword'] != '') {
                    if ($newpassword==$repeatnewpassword)
                    {
                        $querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE   username='$username'");
                        echo "Password changed";
                    }
                    else {echo "New PASSWORDs doesn't match";}

                }


            }
            else {echo "New PASSWORDs doesn't match";}


            if (isset($_POST['email']) AND $_POST['email'] != '') {
                $querychange = mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");
                echo "EMAIL changed";
            }}


        else
        {

            echo"
        <form class='search1' action='changepassword.php' method='POST'>
        <label>Current Password:</label> <input type='password' id='password' name='oldpassword'><p>
        <label>New Password:</label> <input type='password' id='password' name='newpassword'><p>
        <label>Repeat New Password:</label> <input type='password'  name='repeatnewpassword'><p>
        <label>Email:</label> <input type='email'  name='email'><p>
        <input type='submit' name='submit' class='submit' value='submit'><br><br><br>
        <h2><p><a href='index2.php'>Back</a></p></h2>
        </form>
        ";


        }}
    else
        die ("You must be logged in to change your password");


    ?>

<img src="../images/main.jpg">