PHP登录脚本总是失败

时间:2015-03-10 05:36:08

标签: php mysqli

我正在尝试构建一个php生日系统,我需要用户登录才能使用该服务。

我的代码出了问题。我写了这段代码,对我来说重要的是当用户信息不正确时,php应该检查我的数据库,并说如果正确的php echo登录成功则登录失败。

我的问题是当我输入正确的登录电子邮件和密码时,php仍然说登录失败。请帮忙。下面是我的代码。 请注意:header(“location:index.php?login_attempt = 1”);是否在我的HTML上使用$ _GET登录失败。

<?php 

Session_start();
$Email = $_POST["email"];
$Password = $_POST["password"];
$cn ="localhost";
$db_username = "root";
$pas= "";
$db_name = "cemembers";

//Open a connection to a MySQL Server
if($Email && $Password)
{
$connect = mysqli_connect($cn,$db_username,$pas, $db_name) or die("Could not connect to database");


//sending MySqli query
$query = mysqli_query("SELECT * FROM users WHERE Email= '$Email'");
$numrows = mysqli_num_rows($query);



//After PHP, crate index file[form]

    if($numrows !==0)
    {
    while($row= mysqli_fetch_array($query))
    {
        $dbEmail    = $row["Email"];
        $dbPassword = $row["Password"];

        }

        if($Email == $dbEmail && $Password == $dbPassword)
        {

        echo "Login Successful";
        @$_SESSION("Email")== $Email;

            }
        else 
            header("location:index.php?login_attempt=1");

        }
        else
            die ("User is not a member.");  
    }
    else 
        header("Location:index.php?login_attempt=0");


?>

这是我的HTML

<form action="login.php" method="POST" name="MemForm" id="MemForm">
      <fieldset>
        <legend>Christ Embassy Birthday System .</legend>
        <table width="100%" border="0" cellspacing="-">
          <tr>
            <td width="362"><input name="email" type="email" id="email" onFocus="ClearMe(this);" onBlur="unClearMe(this);" placeholder="Email Address"></td>
          </tr>
          <tr>
            <td><input name="password" type="password" id="password" placeholder="Password"></td>
          </tr>
          <tr style="width:50%">
            <td><input name="submit" type="submit" id="submit" form="MemForm" value="Login">

         </td></tr>


        </table>
        <div class="fluid vvi"><p class="enti"><a href="#">Forgotten Password? </a></p>       
          <p class="enti">CE: GYLAM.</p>
        </div>
      </fieldset>

      <?
        $f1=$_GET["login_attempt"];
        if ($f1=="1") {
        echo ('<span class="error">Sorry. Login Fail</span>');

                                        }

?>
  <?
        $f2=$_GET["login_attempt"];
        if ($f2=="0") {
        echo ('<span class="error">Please Enter Email and Password.</span>');

                                        }

?>
      <footer class="fluid MainFooter">
        <p id="cprigt">Copyright&copy; CE. 2015.</p> <p class="powerdby"> Powerd by: XtraDesign.</p>
      </footer>





    </form>

3 个答案:

答案 0 :(得分:1)

请在下面的查询中传递数据库连接链接并尝试。

$query = mysqli_query($connect,"SELECT * FROM users WHERE Email= '$Email'");

当我执行没有连接链接的脚本时,我收到了以下警告

Warning: mysqli_query() expects at least 2 parameters, 1 given in /opt/lampp/htdocs/examples/test1.php on line 17

传递连接后,您的脚本运行正常。希望这个能帮助你。

答案 1 :(得分:0)

首先检查您的数据库连接,然后检查数据库中的密码格式,如果它的md5然后要在查询中使用

$query = "SELECT * FROM users WHERE Email= '$Email'";
$result = $conn->query($query);
$numrows = mysqli_num_rows($result);

答案 2 :(得分:0)

感谢你们...... @Sameer K.非常感谢你们,你们的答案对我和我的代码起了作用,现在通过连接后工作正常。

$query = mysqli_query($connect,"SELECT * FROM users WHERE Email= '$Email'");

5个月到PHP和MYSQL。