PHP if else echo问题

时间:2014-10-29 03:39:32

标签: php indentation

我基本上完成了我的更改密码表单。我只有一个问题。当用户想要更改密码时,它会要求输入“当前密码”,“新密码”和“密码”。和'再次输入新密码'。所以这是我的错误:当我输入“当前密码”时并且它在数据库中是正确的,然后我点击提交,没有任何显示在“错误”中。如果你愿意的话。我希望它基本上显示:'请填写整个表格'

图片1:

enter image description here

图片2:

enter image description here

在图片2上,它应显示在家中'请填写整个表格'

这是我的代码:

<?php
session_start();



$username = $_SESSION['sess_user'];

    echo '<div class="search1"><h2>'.$username.'</h2><div class="search12"><h2><a href="index.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']);


            $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'])) 

             else {echo "<div class='results'>Please fill out the whole form</div><div class='successmate'><h2><p><a href='changepassword.php'><br><br></a></p></h2></div>";}

            {



                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' required><p>
        <label>New Password:*</label> <input type='password' id='password' name='newpassword'><p>
        <label>Repeat New Password:*</label> <input type='password'  name='repeatnewpassword'><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)

我认为问题在于您的代码格式。

正如我从您的代码中所理解的那样,这一部分:

        //check passwords
        if ($oldpassword==$oldpassworddb && !empty($_POST['oldpassword'])) 

         else {echo "<div class='results'>Please fill out the whole form</div><div class='successmate'><h2><p><a href='changepassword.php'><br><br></a></p></h2></div>";}

        {

似乎在语法上搞砸了。

您需要按以下方式重新安排和清理代码:

       <?php
       session_start();

           //opening if and other stuff you'll do here

           //check passwords and your code correction done here
           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>";//I removed an extra curly brace here

                   }

               }
               else 
               {
                   echo "<div class='results'>Please fill out the whole form</div><div class='successmate'><h2><p><a href='changepassword.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>";
           }


          //do your rest of the work here and end your opening if


?>

老实说,你的代码看起来很糟糕。优秀开发人员的一个标志是他/她使代码可读,并且您使代码可读的方法之一是在代码块中遵循适当的缩进。你所有的if-else块都看起来很糟糕,你似乎并不关心缩进它们。结果?你可以亲眼看看。你的代码都搞砸了,我很难理解你的if-else块,它们在哪里开始,在哪里结束。您应该非常认真地浏览以下链接:http://www.riedquat.de/prog/style

答案 1 :(得分:0)

这里有错误的逻辑流程......

你说... if ($oldpassword==$oldpassworddb && !empty($_POST['oldpassword'])) else { ... } { ... } ......没有意义。如果有的话,那么这样做,否则应该是声明。