mysql错误无法正确显示

时间:2014-10-11 05:22:18

标签: php mysql mysqli

我正在按照本教程创建我正在处理的注册数据库。我一直在回顾视频,然后查看试图找到问题解决方案的评论来回反复。

我遇到的问题是我设置的错误没有正确显示我的数据库。我有一个用户名为'lowheartrate'的用户名,但当我将其放入用户名字段并尝试使用它登录时,它会告诉我其中一个其他错误,
“我们找不到那个用户名。你注册了吗?”

很高兴有效,但我也需要其他人工作。具体而言,那些不起作用的是以下错误:

   The 'user_active' error.  

   The 'user_exists' error.

以下是与我遇到的问题有关的代码:

我的login.php,它显示代码的错误: enter image description here

用户连接在一起的users.php代码,告诉您该做什么: enter image description here

general.php: enter image description here

init.php:
enter image description here

更好地了解users.php:

<?php
function user_exists($username) {
    $username = sanitize ($username);
    return  (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"),0) == 1) ? true : false;
    }

function user_active($username) {
    $username = sanitize ($username);
    return  (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `active` = 1"), 0) == 1) ? true : false;
    }
?>

更好地了解login.php:

<?php
include ('core/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.';
    } else if (user_exists($username) == false) {
        $errors[] = 'We can\'t find that username. Have you registered?';
    } else if (user_active($username) == false) {
        $errors[] = 'You haven\'t activated your account.';
    } else {
        // here
    }

    print_r($errors);
}
?>

更好地了解init.php:

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

require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';

$errors = array();
?>

更好地了解general.php:

<?php
function sanitize($data) {
    return mysql_real_escape_string($data);
}
?>

更好地了解connect.php:

<?php
$connect_error = 'Sorry we\'re experiencing connection issues.';
$con = mysqli_connect('localhost','root','');
mysqli_select_db($con,'lr') or die($connect_error);
?>

查看表单(login.php小部件):

<div class="widget">    
    <h2>Returning Member?</h2>
    <div class="inner">

        <form action="login.php" method="post">
            <ul id="login">
                <li>
                    <input type="text" name="username" placeholder="Username" autofocus  >
                </li>

                <li>
                    <input type="password" name="password" placeholder="Password"  >
                </li>

                <li>
                    <input type="submit" value="Log in" >
                </li>

                <li>
                    <a href="register.php">Create an Account</a>
                </li>
            </ul>
        </form>

    </div>
</div>

不确定是否重要但是这里显示的是index.php:

<?php
$pageTitle = 'gLounge - Welcome to gLounge!';
include('includes/php/header.php');
?>

            <!-- Middle content section -->
            <div class="middle">
                <div class="container">
                    <div class="col-md-9 content">
                        <h2>Use gLounge42 as your new source to connect with fellow gamers. We have plenty for you to look at.</h2>
                        <p>

                            gLounge42 allows all types of gamers on all types of consoles/platforms connect with each other with 
                            unlimited amount of reasons to do so. Here at gLounge42 you can chat with them and go as far as posting 
                            in forums to get members for you Call of Duty clan. Possibilities are endless so go ahead and get your 
                            account setup now with gLounge42!

                        </p>
                        <div class="to-tutorial">   
                            <p><strong>Register an Account to get started:</strong></p>
                            <a href="register.php" class="btn btn-success">Register</a>
                        </div>
                    </div>
                    <!-- Includes the login widget on the top right side of page -->
                <?php include('includes/widgets/login.php');?>
                    <!-- Includes the side navigation area --> 
                <?php include('includes/php/sideNavigation.php');?>
                </div>
            </div>
<?php
include('includes/php/footer.php')
?>      

我正在使用的数据库元素:

enter image description here

连接到数据库的用户:

enter image description here

如果需要更多信息,请告知我们 最好的问候,
CODI

2 个答案:

答案 0 :(得分:0)

一切都停止使用mysql。将mysqli_ *函数与预准备语句或PDO一起使用。

您在login.php中的if语句看起来很好。尝试在user.php中使用它:

$bool = (mysql_num_rows(mysql_query("your query")) == 1) ? true : false; 

现在您正在检查已返回的行数。我不确定你是否可以在回报中加上if语句。所以现在你做了:

Return $bool;

答案 1 :(得分:0)

<强>的init.php

<?php
session_start();
error_reporting(E_ALL);//I change this line always use this. During 
//the production. after you finish you can change it. 

require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';

$errors = array();
?>

阅读评论,您在答案中找到了它们。 Connect.php

<?php
//Your were mixing the mysqli and mysql, here you are using mysqli_ function But method you are 
//using msysql one so change it to mysql function



$connect_error = 'Sorry we\'re experiencing connection issues.';
$con = mysql_connect('localhost','root','');
mysql_select_db('lr',$con) or die($connect_error);//Database name should be first
?>

<强> users.php

<?php

require 'database/connect.php';//You were missing connection here.
function user_exists($username) {
    $username = sanitize ($username);
    echo $username;
    //$result=or die(mysql_error());

    if(mysql_num_rows(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"))==1)        return TRUE; else     return FALSE;
    //if(mysql_num_rows(mysql_query()== 1)) 
    }

function user_active($username) {
    $username = sanitize ($username);
if(mysql_num_rows(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username' AND `active`='1'" ))==1)        return TRUE; else     return FALSE;    }
?>