使用php和SQL检查数据库中的数据的用户输入时出错

时间:2014-03-12 01:10:48

标签: php sql database forms login

我在php中有一个登录表单,我已经连接到phpmyadmin中的sql数据库。我编写的脚本应该从用户那里获取电子邮件和密码变量。然后应检查数据库以确保用户已注册。然后将用户重定向到他们的帐户或通知用户他们的详细信息没有匹配,并要求他们再试一次。但是,无论我以何种方式更改代码,即使我通过表单提交的数据是随机信息,它也会始终重定向到帐户页面。

任何帮助或建议都会很棒。谢谢。 这是我的代码

<?php
//require_once 'includes/db_connect.php';//
//require_once 'includes/functions.php';//

$con=mysqli_connect("x","m","z","m");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

session_start(); //starting a PHP session.


if (isset($_SESSION['logged'])){
    header("Location:account.html");
    exit();
}



    $email = $_POST['email'];
    $password =($_POST['password']); 

    $sql = mysqli_query($con,"SELECT * FROM `websiteusers` WHERE email ='" . $email . "' and password ='" . $password . "'") or die (mysqli_error($sql));
    $count = mysqli_num_rows($sql);
    //$row=mysqli_fetch_array($query);//



     if ($count==1){
        $_SESSION[logged] = 1;
        $_SESSION['email'] = $email;
     $_SESSION['password'] =$password;
         header("location:account.html"); /* Redirect the browser */
    }  

        else {
       echo "Sorry those details are not in the database. Click here <a href=\"memberlogin.html\"</a> to try again.";  
        }


exit();

?>

2 个答案:

答案 0 :(得分:0)

这是因为您使用的会话索引$_SESSION[logged]应该是$_SESSION['logged']

if ($count==1){
    $_SESSION['logged'] = 1;
    $_SESSION['email'] = $email;
    $_SESSION['password'] =$password;
    header("location:account.html"); /* Redirect the browser */
}  

答案 1 :(得分:0)

您忘记了''变量中的$_SESSION[logged],它应该是$_SESSION['logged']