尝试允许PHP为空

时间:2014-10-24 12:20:52

标签: php mysql

我只是想让用户无需在上面输入任何内容即可更改自己的电子邮件。目前它的工作原理,但它也说'当前密码不正确/错误'。我可以忽略'当前密码不正确/错误'如果我只是想要更改电子邮件?

谢谢!

图片:http://puu.sh/cmcKM/aef56cdaf4.png

我已尝试过使用此功能,但没有成功仍然给了我相同的信息

if (!empty($_POST['repeatnewpassword']) && !empty($_POST['newpassword'])) {
            if ($newpassword==$repeatnewpassword)
            {
                $querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE   username='$username'");
                echo "<div class='successmate'><br><br><br><br><hr>Password has been changed!</hr></div><div class='successmate'><br><hr><br><h2><p><a href='index2.php'><br><br></a></p></h2></div>";
            }
            else {echo "<div class='results'>new password(s) dont match</div><div class='successmate'><br><br><h2><p><a href='changepassword.php'>try again?</a></p></h2></div>";}

        }

我的代码:

<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 class="search12"><h2><a href="index2.php">Home</a></h2></p></div></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 && !empty($_POST['oldpassword']))
            {

                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 "<div class='successmate'><br><br><br><br><hr>Password has been changed!</hr></div><div class='successmate'><br><hr><br><h2><p><a href='index2.php'><br><br></a></p></h2></div>";
                    }
                    else {echo "<div class='results'>new password(s) dont match</div><div class='successmate'><br><br><h2><p><a href='changepassword.php'>try again?</a></p></h2></div>";}

                }


            }
            else {echo "<div class='results'>current password doesnt match</div><div class='successmate'><h2><p><a href='changepassword.php'><br><br>Try again?</a></p></h2></div>";}


            if (isset($_POST['email']) AND $_POST['email'] != '') {
                $querychange = mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");
                echo "<div class='successmate'><br><br><br><br><hr>Your email has been changed</hr></div><div class='successmate'><br><hr><br><h2><p><a href='index2.php'><br><br></a></p></h2></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">

编辑 - 弗雷德:

<?php
session_start();



$username = $_SESSION['sess_user'];

    echo '<div class="search1"><h2>'.$username.'</h2><div class="search12"><h2><a href="index2.php">Home</a></h2></p></div></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($_POST['newpassword']){
            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 "<div class='successmate'><br><br><br><br><hr>Password has been changed!</hr></div><div class='successmate'><br><hr><br><h2><p><a href='index2.php'><br><br></a></p></h2></div>";
                    }
                    else {echo "<div class='results'>new password(s) dont match</div><div class='successmate'><br><br><h2><p><a href='changepassword.php'>try again?</a></p></h2></div>";}

                }


            } else {echo "<div class='results'>current password doesnt match</div><div class='successmate'><h2><p><a href='changepassword.php'><br><br>Try again?</a></p></h2></div>";}
        }

        if (isset($_POST['email']) AND $_POST['email'] != '') {
            $querychange = mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");
            echo "<div class='successmate'><br><br><br><br><hr>Your email has been changed</hr></div><div class='successmate'><br><hr><br><h2><p><a href='index2.php'><br><br></a></p></h2></div>";
        }
  }


            }
            else {echo "<div class='results'>current password doesnt match</div><div class='successmate'><h2><p><a href='changepassword.php'><br><br>Try again?</a></p></h2></div>";}


            if (isset($_POST['email']) AND $_POST['email'] != '') {
                $querychange = mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");
                echo "<div class='successmate'><br><br><br><br><hr>Your email has been changed</hr></div><div class='successmate'><br><hr><br><h2><p><a href='index2.php'><br><br></a></p></h2></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");


    ?>

2 个答案:

答案 0 :(得分:1)

确保您检查是否有人输入了密码,如果是,您更改了密码,否则您只需更改电子邮件

像这样:

 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($_POST['newpassword']){
            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 "<div class='successmate'><br><br><br><br><hr>Password has been changed!</hr></div><div class='successmate'><br><hr><br><h2><p><a href='index2.php'><br><br></a></p></h2></div>";
                    }
                    else {echo "<div class='results'>new password(s) dont match</div><div class='successmate'><br><br><h2><p><a href='changepassword.php'>try again?</a></p></h2></div>";}

                }


            } else {echo "<div class='results'>current password doesnt match</div><div class='successmate'><h2><p><a href='changepassword.php'><br><br>Try again?</a></p></h2></div>";}
        }

        if (isset($_POST['email']) AND $_POST['email'] != '') {
            $querychange = mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");
            echo "<div class='successmate'><br><br><br><br><hr>Your email has been changed</hr></div><div class='successmate'><br><hr><br><h2><p><a href='index2.php'><br><br></a></p></h2></div>";
        }
  }

答案 1 :(得分:0)

Ariana你的代码有点令人困惑,我认为你的代码中有一点丢失了你的其他条件。我重写了它,如果有效的话就试试。

**更新了***

你真的不应该使用它,因为它不安全 - 尝试使用mysqli_而不是mysql_并了解mysql_real_escape_string。无论如何,我已经用我的评论更新了代码,它应该可以工作。

<?php
session_start();

$username = $_SESSION['sess_user'];
    echo '<div class="search1"><h2>'.$username.'</h2><div class="search12"><h2><a href="index2.php">Home</a></h2></p></div></div>';


    if (isset($username)){
        //user is logged in

        if (isset($_POST['submit'])){

            #1 Check if fields are not empty
            if( !isset($_POST['oldpassword']) || !isset($_POST['newpassword']) ){
                echo "Fields empty";    
                exit();
            }

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

            //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'];

            #2 Check the old password

            if($oldpassword==$oldpassworddb){
            # they match  - check if nwe and repeat pass match

                if($newpassword == $repeatnewpassword){
                    mysql_query("UPDATE login SET password='$newpassword' WHERE username='$username' ");

                echo "<div class='successmate'><br><br><br><br><hr>Password has been changed!</hr></div><div class='successmate'><br><hr><br><h2><p><a href='index2.php'><br><br></a></p></h2></div>";

                # check if email isn't empty so you can update

                if( !empty($_POST['email']) ){

                    mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");

                     echo "<div class='successmate'><br><br><br><br><hr>Your email has been changed</hr></div><div class='successmate'><br><hr><br><h2><p><a href='index2.php'><br><br></a></p></h2></div>";
                }



                }else{
                    echo "<div class='results'>Passwords don't match !</div><div class='successmate'><br><br><h2><p><a href='changepassword.php'>try again?</a></p></h2></div>";
                }


            }else{
            # they don't match

                echo "<div class='results'>Old password wrong !</div><div class='successmate'><br><br><h2><p><a href='changepassword.php'>try again?</a></p></h2></div>";

            }


            } // end if form submitted
            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">