2012-07-06 67 views
0

我目前正在嘗試創建註冊表單,並且我有表單本身工作,並且人們可以在我的數據庫中創建用戶,但是當他們註冊並重定向它們時到admin.php在PHP中使用mysql_query顯示用戶名不起作用

他們用來創建帳戶的名稱不會按行用戶名顯示。它應該說「歡迎,user_name,你現在登錄!」

我只是不能讓名字出現,但其他一切正常!

警告:mysql_fetch_array()預計參數1是資源,在布爾C中給出:\路徑\到\上線admin.php的25
警告:mysql_fetch_array()預計參數1是資源,布爾給定在C:\路徑\到\上線的login.php 36

管理員:

<?php 
require('db_config.php'); 
require_once('functions.php'); 

//if the cookie is still valid, recreate the session 
if($_COOKIE['logged_in'] == true){ 
    $_SESSION['logged_in'] = true; 
    $_SESSION['user_id'] = $_COOKIE['user_id']; 
    $_SESSION['is_admin'] = $_COOKIE['is_admin']; 

} 
if($_SESSION['logged_in'] != true){ 
    //not logged in! send them back to the form] 
    header('location:login.php'); 
} 

//extract the data for the logged in user, so we can use it on all page 
$user_id = $_SESSION['name']; 
$query_user = "SELECT * FROM users 
       WHERE name = $user_id 
       LIMIT 1"; 

$result_user = mysql_query($query_user); 
$row_user = mysql_fetch_array($result_user); 
//this going to be a handy variable to have throughout all pages 
$user_id = $row_user['user_id']; 

?> 
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<meta charset="utf-8"> 
<link rel="stylesheet" type="text/css" href="css/reset.css" /> 
<link rel="stylesheet" type="text/css" href="css/format.css" /> 
<title>Schell Shock Design's Portfolio</title> 
</head> 

<body> 
<div id="login"> 
<?php 
    include('login.php'); 
    ?> 
    </div> 
    <div id="utilities"> 
    <?php include('utilities.php'); ?> 
    </div> 

<div id="container"> 
    <header> 
    <?php include('header.php'); ?> 
    </header> 
     <div id="slider"> 
     <?php include('slider.php'); ?> 
      </div> 
     <div id="content"> 
     <?php include('content.php'); ?> 

    </div> 
    <div id="bottomcontent"> 
     <?php include('bottomcontent.php'); ?> 
    </div> 
    <div id="footer"> 
     <?php include('footer.php'); ?> 
</div> 
</body> 
</html> 

登錄:

<?php 
//show an error if there is a problem with the login 
if($error == true){ ?> 

    <div class="error"> 
     Sorry, Your username and password are incorrect. Try again. 
    </div> 

<?php } //end if error ?> 


<?php //show the form only if NOT logged in 
if(!$_SESSION['logged_in']){ 

?> 
    <div class="form1"> 
    <form action="?action=" method="post"> 
    <label for="username">Username:</label> 
    <input type="text" name="username" id="username" /> 
    <label for="password">Password</label> 
    <input type="password" name="password" id="password" /> 
    <input type="submit" value="Log in" /> 
    <input type="hidden" name="did_login" value="1" /> 
</form> 
<?php } //end if not logged in 

else{ 
//get info of logged in person 
    $user_id = $_SESSION['user_id']; 
    $query_user = "SELECT name 
        FROM users 
        WHERE user_id = $user_id"; 

$result_user = mysql_query($query_user); 
$row_user = mysql_fetch_array($result_user); 
?> 
    <div id="loggedin"> 
    <a href="?action=logout">Log Out</a> 

    <?php //show a welcome message if they logged in successfully 
    echo 'Welcome '.$row_user['name'].', You are now logged in!'; 
?> 


<?php } ?> 
</div> 

註冊

<?php 
//register parse. all this logic MUST go before the doctype or any other text output. 
require('db_config.php'); 
require_once('functions.php'); 

//if they submitted the form, parse it 
if($_POST['did_register'] == 1){ 
    //extract amd sanitize all fields 
    $username = clean_input($_POST['username']); 
    $email = clean_input($_POST['email']); 
    $password = clean_input($_POST['password']); 
    $repassword = clean_input($_POST['repassword']); 
    $policy = clean_input($_POST['policy']); 

    //encrypted version of the password, for storing in the database 
    $sha_password = sha1($password); 

    //begin validation 
    $valid = true; 

    //did they forget to check the box? 
    if($policy != 1){ 
     $valid = false; 
     $msg = 'You must agree to the TOS and PP before signing up. <br />'; 
    } 

    //repeated password does not match 
    if($password != $repassword){ 
     $valid = false; 
     $msg .= 'The passwords provided do not match. <br />'; 
    } 

    //make sure the username and password are at least 5 characters long, than check the database 
    if(strlen($username) >= 5 AND strlen($password) >= 5){ 
     //check to see if username is already taken 
     $query_username = "SELECT name 
          FROM users 
          WHERE name = '$username' 
          LIMIT 1"; 

     $result_username = mysql_query($query_username); 
     //if one result is found, username is taken. 
     if(mysql_num_rows($result_username) == 1){ 
      $valid= false; 
      $msg .= 'That username is already taken. Try another. <br />'; 
     } 
    }else{ 
     $valid = false; 
     $msg .= 'Username and Password must be at least 5 characters long. <br />'; 
    } 

    //check for valid email, than check for match in database 
    if(check_email_address($email) == true){ 
     //look for match in database 
     $query_email = "SELECT email 
         FROM users 
         WHERE email = '$email' 
         LIMIT 1"; 
     $result_email = mysql_query($query_email); 
     //if 1 result is found, email is taken. 
     if(mysql_num_rows($result_email) == 1){ 
      $valid = false; 
      $msg .= 'Looks like an account with that email already exists. Do you want to login? <br />'; 

     } 
    }else{ 
     //invalid email 
     $valid = false; 
     $msg .= 'Please provide a valid email address. <br />'; 
    } 

    //if the data passed ALL tests, add the user to the database 
    if($valid == true){ 
     $query_insert = "INSERT INTO users 
         (name, password, email, join_date, is_admin) 
         VALUES 
         ('$username', '$sha_password', '$email', now(), 0)"; 

     $result_insert = mysql_query($query_insert); 
     //check to see if it worked 
     if(mysql_affected_rows() == 1){ 
      //SUCCESS! Log the user in and send them to their profile. 
      $_SESSION['logged_in'] = true; 
      setcookie('logged_in', 'true', time() + 60*60*24*7); 
      header('location:index.php'); 

     }else{ 
      $msg .= 'There was a problem adding the user to the Database'; 
     } 
    } 
} //end if submitted form 
?> 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Sign up for an account</title> 

</head> 

<body> 
    <?php 
    if(isset($msg)){ 
     echo $msg; 
    } 
    ?> 
    <form action="registration.php" method="post"> 
     <label for="username">Choose a Username:</label> 
     <input type="text" name="username" id="username" /> 
     <span class="hint">Minimum of five characters</span> 

     <label for="email">Your Email Address:</label> 
     <input type="text" name="email" id="email" />  

     <label for="password">Choose a Password:</label> 
     <input type="password" name="password" id="password" /> 
     <span class="hint">Minimum of 5 characters</span> 

     <label for="repassword">Repeat Password:</label> 
     <input type="password" name="repassword" id="repassword" /> 

     <input type="checkbox" name="policy" id="policy" value="1" /> 
     <label for="policy">Yes, I have read the Terms of Service and Privacy Policy.</label> 

     <input type="submit" value="Sign up" /> 
     <input type="hidden" name="did_register" value="1" /> 
    </form> 


</body> 
</html> 

我需要做什麼來解決?

+2

'$ query_user'查詢失敗,因此你'FALSE'(這是布爾值),而不是行集。我真正想知道的是爲什麼你選擇包含所有不相關的東西而不是這個查詢 - 這裏唯一重要的東西。 – raina77ow 2012-07-06 23:54:03

+0

也許'fetch_assoc'而不是'fetch_array'? – 2012-07-06 23:54:21

+3

如果SO大師將網站的標識更改爲「mysql庫已被貶低,爲了上帝的緣故,人們!」,這將會被人們所讚賞的話。 – raina77ow 2012-07-06 23:55:57

回答

0

admin.php,該查詢失敗:

$query_user = "SELECT * FROM users WHERE name = $user_id LIMIT 1"; 

也許$user_id是空的,或者它需要被引用('$user_id')。

在任何情況下,你應該檢查查詢的結果,以確保它是成功的:

$user_id = $_SESSION['name']; 
$query_user = "SELECT * FROM users 
       WHERE name = $user_id 
       LIMIT 1"; 

$result_user = mysql_query($query_user); 
if (!$result_user) { 
    die('Query failed: ' . mysql_error()); 
} 

mysql_query()僅成功返回的資源結果。如果失敗,則返回(bool)FALSE,這不能傳遞給任何mysql_fetch_*函數。

login.php中的錯誤也是如此。

您似乎沒有顯示在登錄時運行的代碼,我的猜測是您沒有爲會話分配正確的變量。在每個頁面的頂部

if (!$result_user) { 
    die('MySQL Error: '.mysql_error()); 
} 
  • 呼叫session_start()

  • 1
    1. 您應該檢查錯誤是什麼。

    2. ,並確保會議的價值觀是正確返回:

      print_r($_SESSION);