好吧,这里出了点问题。我输入数据库中存在的用户名和密码。在这种情况下,应echo该字符串


  需要激活


但它呼应


  您需要注册


init.php

<?php
    //error_reporting(0);
    session_start();

    require 'dbconnect.php';//this works okay so i wouldn't post this file code
    require 'users.php';

    $errors = array();
?>


users.php

        <?php

        function user_exists($username){
            $username = mysql_real_escape_string($username);

            $query = mysql_query("SELECT COUNT('user_id') FROM `users` WHERE 'username' = '$username'");

            if (!$query) {
                die('Could not query:' . mysql_error());
            }

            return (mysql_result($query, 0) == 1) ? true : false;

        }

        function user_active($username){

            $username = mysql_real_escape_string($username);

            $querytoo = mysql_query("SELECT COUNT('user_id') FROM `users` WHERE 'username' = '$username' AND 'active' = 1");

            if (!$querytoo) {
                die('Could not query:' . mysql_error());
            }

            return (mysql_result($querytoo , 0) == 1) ? true : false;

        }
    ?>


login.php

<?php
include 'init.php';


if(empty($_POST) === false){
    $username = $_POST['username'];
    $password = $_POST['password'];

    if(empty($username) === true || empty($password) === true){
        $errors[]='You need to enter a username and password';
    }
    elseif(user_exists($username) === false){
        $errors[]='You need to reg';
    }
    elseif(user_active($username) === false){
        $errors[]='need to activate';
    }
    else {
    //
    }

    print_r($errors);
}



?>


HTML的一部分

<form action="login.php" method = "post">
                    <ul id="login">
                        <li>
                            username:<br>
                            <input type="text" name="username" size="30" value=""/></li>
                        <li>password:<br>
                            <input type="password" name="password" size="30" value=""/></li>
                        <li><input type = "submit" value ="Log in"></li>
                        <li>
                            <a href="register.php"> Register</a>
                        </li>
                    </ul>


附言
查询文本工作正常,我在mysql中对其进行了检查。当我在这里输入``而不是''时在PHP代码中


  从users中选择COUNT('user_id')个'username'='$ username'
  AND'active'= 1


出现


  “无法查询:”


事情

我尝试了elseifelse if的东西,所以我不认为存在问题

最佳答案

您的SQL错误。您正在将静态字符串与您的值而不是列名进行比较:

    SELECT COUNT('user_id') FROM users WHERE 'username' = '$username' AND 'active' = 1


应该

SELECT COUNT(user_id) FROM users WHERE username = '$username' AND active = 1


请注意,我从列名中删除了“。使用反引号也有效:

SELECT COUNT(`user_id`) FROM users WHERE `username` = '$username' AND `active` = 1

10-08 02:32