我使用外部文件连接数据库,我也有一个登录文件。登录文件应该将用户输入与数据库记录进行比较。目前,在提交数据后,页面只是空白,甚至不显示错误。
注册表格工作正常,所以我不认为连接是问题所在。
PHP:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'connect.php';
session_start();
$error = ""; //Variable for storing our errors.
if(isset($_POST["login-submit"]))
{
if(empty($_POST["username"]) || empty($_POST["password"]))
{
$error = "Both fields are required.";
}else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// To protect from MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
//Check username and password from database
$sql="SELECT UserID FROM user WHERE Email='$username' and Password='$password'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
//If username and password exist in our database then create a session.
//Otherwise echo error.
if(mysqli_num_rows($result) == 1)
{
$_SESSION['username'] = $login_user; // Initializing Session
header("location: http://localhost/alisonhurmanwebsite/index.html"); // Redirecting To Other Page
}else
{
$error = "Incorrect username or password.";
}
}
}
?>