0

我有一个简单的表格来注册完美运行的用户。现在我正在尝试使用任何注册用户登录,但我无法使其正常工作。我一直在阅读有关有同样问题的人的文章,但我找不到解决方案。

这是我的代码:

连接.inc.php

    <?php

$name = 'pruser';
$pwd = 'pruser';
$dbname = 'project1db';
$conn = new mysqli("localhost", $name, $pwd, $dbname);

登录.php

    <?php
include('connection.inc.php');
if (isset($_POST['login'])) {
    session_start();
    $username = trim($_POST['user']);
    $password = trim(sha1($_POST['pass']));

    // check user in db
    $sql = "SELECT user, pwd FROM users WHERE user = ? AND pwd = ? LIMIT 1";
    $stmt = $conn->prepare($sql);
    if ($stmt === false) {
        echo 'Error';
    }
    $stmt->bind_param('ss', $username, $password);
    $stmt->execute();
    $stmt->bind_result($username, $password);
    echo $stmt->num_rows;
    $stmt->close();
}
?>


<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Log in - Project 1</title>
</head>
<body>

<form id="form1" method="post" action="">
    <fieldset>
        <legend>Log in</legend>
        <p>
            <label for="user">Username</label>
            <input type="text" name="user" id="user">
            <label for="pass">Password</label>
            <input type="password" name="pass" id="pass">
        </p>
        <p>
            <input type="submit" name="login" id="login" value="Log in">
        </p>
    </fieldset>
</form>

<a href="register.php">Register</a/>
</body>
</html>

当我检查有多少行受到影响时,结果是-1。登录后,我希望它重定向到 index.php。这不是问题,但首先我需要登录。

4

2 回答 2

0

尝试$stmt->store_result();之后立即调用$stmt->execute();

于 2013-08-18T21:31:43.123 回答
0
      $sql = "SELECT count(*) FROM users WHERE user = ? AND pwd = ? LIMIT 1";
        $stmt = $conn->prepare($sql);
        if ($stmt === false) {
            echo 'Error';
        }
        $stmt->bind_param('ss', $username, $password);
        $stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch;
        $stmt->close();
        if($count > 0){
## login OK
        } else {
## login false
        }
于 2016-02-05T18:30:55.450 回答