在php和编辑登录页面

时间:2016-11-04 14:49:44

标签: php mysql

我想创建一个登录页面,以表格形式显示来自特定用户数据库的数据,最多4列和1行,并且应该能够更新表格数据然后注销。任何人都可以用脚本帮助我。我需要使用php,mysql来完成它。我已经尝试过这个,但我不知道该怎么做。

<html>
<form action="login.php" method="post">
    Username: <input type="text" name="username"><p>
    Password: <input type="password" name="password"><p>
              <input type="submit" value="Log in!" >
</form>           

<?php

session_start();

$username = $_POST['username'];
$password = $_POST['password'];

if ($username&&$password) 
{
    $connect = mysql_connect("localhost", "root", "") or die("Couldn't connect to the database!");
    mysql_select_db("login") or die("Couldn't find database");

    $query = mysql_query("SELECT * FROM users WHERE username='$username'");

    $numrows = mysql_num_rows($query);

    if ($numrows!==0)
    {
        while($row = mysql_fetch_assoc($query)) 
        {
            $dbusername = $row['username'];
            $dbpassword = $row['password']; # code...
        }

        if ($username==$dbusername&&$password==$dbpassword)
        {
            echo "You are logged in!";# 
            @$_SESSION['usernme'] = $username;
        }
        else
            echo "Your password is incorrect!";
    # code...
    }
    else
        die("That user doesn't exists!");

}
else
    die("Please enter a username and password!")
?>

1 个答案:

答案 0 :(得分:0)

为什么不直接让MySQL检查用户名和密码的组合是否正确?

$query = mysqli_query("SELECT * FROM users WHERE username='" . $username . "' AND password = '" . $password . "'");

仅在mysqli_num_rows($query) == 1时才有有效登录信息。不应该有超过1行。