我想为我的php页面创建一个登录 - 与数据库连接但无法正常工作

时间:2016-03-11 12:25:57

标签: php mysqli

我想在登录成功后自动打开Home.php页面。

我的数据库名称是relieffoundation,它是在wampserver locoalhost中创建的。表名是admin,它直接显示其他部分,即uername和密码不匹配

<div id="user_login_div_input">
<?php
if (isset($_POST['Username'])) {
    $user = $_POST['Username'];
    $pass = md5($_POST['Password']);

    $con = mysqli_connect("localhost", "root", "");
    if (!$con) {
        die('Could not connect to the database!' . mysqli_errno());
    }
    $selectdb = mysqli_select_db($con, 'relieffoundation') or die("Could not select DataBase");
    $query = "SELECT *FROM admin where username = '$user' AND password= '$pass'";
    if (mysqli_num_rows(mysqli_query($con, $query))) {
        echo "Login successfull";
        header("location : Home.php");
    } else {
        echo "Uername and Password not match!";
    }
    mysqli_close($con);
}
?>
LOG IN
<form  id="myForm" name="myForm"  method="post">      
    <input class="input" type="text" id="Username" name="Username"  placeholder="Username" required="">
    <input class="input" type="password" id="Password" name="Password" placeholder="Password" required="">
    <button id="login" type="submit" > Log In</button>
</form>

  

2 个答案:

答案 0 :(得分:-1)

尝试删除'in query $query = "SELECT *FROM admin where username = $user AND password= $pass"; 或尝试连接 $query = "SELECT *FROM admin where username ='". $user."' AND password='". $pass.'";

答案 1 :(得分:-1)

你有

$query = "SELECT *FROM admin where username = '$user' AND password= '$pass'";

你需要*和FROM之间的空格

$query = "SELECT * FROM admin where username = '$user' AND password= '$pass'";