0

我有一个登录表单,当密码输入错误但电子邮件不正确时会显示一条消息。但是当电子邮件或两者都不正确时,它不会显示任何内容。我希望它在这两种情况下都显示消息(“密码和电子邮件不匹配”)丢失。我真的很感激任何帮助。

  <div id="container">
            <div>
                <h1>Log In</h1>
                <form method="POST" action="logIn.php">
                    <label>Email</label>
                    <input name= "txtEmail"/>
                    <label>Password</label>
                    <input type="password" name="txtPass"/>
                    <input type= "submit" name= "submit" value="Log In"/>
                </form>
            </div>  
        </div>

<?php

    require ('connection.php');

            if (isset($_POST["txtEmail"]))
            {
            $email= $_POST["txtEmail"];//NO RECONOCE LOS TEXT FIELDS!!
            }
            else
            {$email= "";
            echo "Email empty";}

            if (!empty($_POST['txtPass']))
            {
            $pass= $_POST['txtPass'];
            }
            else
            {$pass= "";
            echo "Password empty.";}

            // Enviar consulta
              $instruction = "SELECT Password, Email FROM customer WHERE Email = '". $email . "'";
              $query = mysql_query ($instruction, $connection)
                 or die ("Fallo en la consulta");       
           // Mostrar resultados de la consulta
              $nrows = mysql_num_rows ($query);
              if ($nrows > 0)
              {
                    $result = mysql_fetch_array ($query);
                    if (isset($result['Password']))
                    {
                    $clave = $result['Password'];
                    //echo "Isset password works.";
                    }
                    else {$result= "";}
                    Email doesnt exist, or ;}


                    if ($clave == $pass){
                        header('Location: welcomeLogIn.php');
                    }
                    else{
                        echo "Password or Email doesnt match.";
                    }
              }
    mysql_close($connection);
    ?>
4

2 回答 2

0

感谢您提供代码供我们审核。根据代码,我可以看到您刚刚开始学习 PHP。我已经用一种非常简单的方法更新了你的代码来完成你所要求的事情。基本上,如果电子邮件或密码为空,那么它会将 $empty_value 变量设置为 1。然后在脚本结束时检查 $empty_value 变量是否等于 1。如果是您想要的消息将是打印。请注意,您的代码存在许多安全问题,我建议阅读以下指南中的所有信息:http: //phpsec.org/projects/guide/

<?php

    require ('connection.php');
$empty_value = 0;

            if (isset($_POST["txtEmail"]))
            {
            $email= $_POST["txtEmail"];//NO RECONOCE LOS TEXT FIELDS!!
            }
            else
            {$email= "";
            $empty_value = 1;}

            if (!empty($_POST['txtPass']))
            {
            $pass= $_POST['txtPass'];
            }
            else
            {$pass= "";
            $empty_value = 1;}

            // Enviar consulta
              $instruction = "SELECT Password, Email FROM customer WHERE Email = '". $email . "'";
              $query = mysql_query ($instruction, $connection)
                 or die ("Fallo en la consulta");       
           // Mostrar resultados de la consulta
              $nrows = mysql_num_rows ($query);
              if ($nrows > 0)
              {
                    $result = mysql_fetch_array ($query);
                    if (isset($result['Password']))
                    {
                    $clave = $result['Password'];
                    //echo "Isset password works.";
                    }
                    else {$result= "";}
                    Email doesnt exist, or ;}


                    if ($clave == $pass){
                        header('Location: welcomeLogIn.php');
                    }
                    else{
                        echo "Password or Email doesnt match.";
                    }
              }
    mysql_close($connection);
    ?>

<?php
if ($empty_value == 1) {
echo "Password or Email doesn't match.";
}
?>

  <div id="container">
            <div>
                <h1>Log In</h1>
                <form method="POST" action="logIn.php">
                    <label>Email</label>
                    <input name= "txtEmail"/>
                    <label>Password</label>
                    <input type="password" name="txtPass"/>
                    <input type= "submit" name= "submit" value="Log In"/>
                </form>
            </div>  
        </div>
于 2012-11-13T19:33:42.437 回答
0

如果没有匹配的电子邮件,那么您将不会得到任何结果,因此您可以简单地添加:

if ($nrows > 0)
{
   //You current code to check if the password is correct                 
}
else{
  echo "No matching email found";
}
于 2012-11-13T19:34:23.390 回答