2014-10-11 130 views
0

我正在按照本教程創建我正在使用的註冊數據庫。我回過頭來看視頻,然後看着評論,試圖找到解決我的問題的方法。mysql錯誤顯示不正確

我遇到的問題是,我設置的錯誤不能正確顯示我的數據庫。我有一個用戶名爲'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); 
?> 

一看錶(登錄。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

如果需要任何更多信息,請讓我知道。
此致敬禮,
科迪

+0

@ Arif_suhail_123我仍然得到同樣的錯誤,當我用2代替3 '=' 代替的跡象。沒有拿起user_active不幸 – cscodismith 2014-10-11 05:31:42

+0

@ Arif_suhail_123是的,仍然沒有找到我的解決方案。會愛你的輸入請 – cscodismith 2014-10-11 06:21:38

+0

@ Arif_suhail_123當我添加你給我的代碼並繼續到login.php它沒有任何錯誤代碼什麼都不是什麼好事,因爲我繼續登錄login.php,甚至沒有把一個用戶名或密碼,它並沒有給我一個錯誤代碼。 – cscodismith 2014-10-11 06:32:42

回答

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; } 
?> 
0

首先停止使用mysql的首要。對準備好的語句或PDO使用mysqli_ *函數。

你在login.php中的if語句看起來不錯。嘗試在user.php中使用它:

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

現在您檢查已返回的行數。而且我不確定你是否可以在回報中加入if語句。所以,現在你做的事:

Return $bool; 
+0

好的,謝謝。對不起,我只是不想再搞砸了。我把$ bool =(mysql_num_rows(mysql_query(「你的查詢」))== 1)?真假;在最上面一行還是最下面一行!?你想讓我把你的回報$ bool放在哪裏? ??如果你能給我線條,那將會很棒! – cscodismith 2014-10-11 05:45:01

+0

Put $ bool =(mysql_num_rows(mysql_query(「your query」))== 1)?真假;在user.php中的return語句中,並用return來替換你現有的return語句$ bool – andy 2014-10-11 05:52:21

+0

我目前有兩個返回語句,我感到困惑的是每個語句的放置位置,目前我沒有收到任何錯誤或任何顯示被定向到login.php - 它只是白色,沒有內容,所以如此。 – cscodismith 2014-10-11 05:57:40