mysql - Using mysql_query in PHP to show the user name is not working -


i trying create registration form, , have form working , people can create user's in database, when sign , redirects them admin.php.

the name used create account doesn't show up, down row user name. should "welcome, user_name, logged in!"

i can't name show else works!

warning: mysql_fetch_array() expects parameter 1 resource, boolean given in c:\path\to\admin.php on line 25
warning: mysql_fetch_array() expects parameter 1 resource, boolean given in c:\path\to\login.php on line 36

admin:

<?php require('db_config.php'); require_once('functions.php'); //if cookie still valid, recreate 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 form] header('location:login.php'); } //extract data logged in user, can use on page $user_id = $_session['name']; $query_user = "select * users name = $user_id limit 1"; $result_user = mysql_query($query_user); $row_user = mysql_fetch_array($result_user); //this going handy variable have throughout 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> 

login:

<?php //show error if there problem login if($error == true){ ?> <div class="error"> sorry, username , password incorrect. try again. </div> <?php } //end if error ?> <?php //show form 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 users 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 welcome message if logged in echo 'welcome '.$row_user['name'].', logged in!'; ?> <?php } ?> </div> 

registration

<?php //register parse. logic must go before doctype or other text output. require('db_config.php'); require_once('functions.php'); //if submitted form, parse if( $_post['did_register'] == 1 ){ //extract amd sanitize 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 password, storing in database $sha_password = sha1($password); //begin validation $valid = true; //did forget check box? if( $policy != 1 ){ $valid = false; $msg = 'you must agree tos , pp before signing up. <br />'; } //repeated password not match if( $password != $repassword ){ $valid = false; $msg .= 'the passwords provided not match. <br />'; } //make sure username , password @ least 5 characters long, check database if( strlen($username) >= 5 , strlen($password) >= 5 ){ //check see if username taken $query_username = "select name users name = '$username' limit 1"; $result_username = mysql_query($query_username); //if 1 result found, username taken. if( mysql_num_rows($result_username) == 1 ){ $valid= false; $msg .= 'that username taken. try another. <br />'; } }else{ $valid = false; $msg .= 'username , password must @ least 5 characters long. <br />'; } //check valid email, check match in database if( check_email_address($email) == true ){ //look match in database $query_email = "select email users email = '$email' limit 1"; $result_email = mysql_query($query_email); //if 1 result found, email taken. if( mysql_num_rows($result_email) == 1 ){ $valid = false; $msg .= 'looks account email exists. want login? <br />'; } }else{ //invalid email $valid = false; $msg .= 'please provide valid email address. <br />'; } //if data passed tests, add user database if( $valid == true ){ $query_insert = "insert users (name, password, email, join_date, is_admin) values ('$username', '$sha_password', '$email', now(), 0)"; $result_insert = mysql_query($query_insert); //check see if worked if( mysql_affected_rows() == 1 ){ //success! log user in , send them profile. $_session['logged_in'] = true; setcookie( 'logged_in', 'true', time() + 60*60*24*7 ); header( 'location:index.php' ); }else{ $msg .= 'there problem adding user 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 account</title> </head> <body> <?php if( isset($msg) ){ echo $msg; } ?> <form action="registration.php" method="post"> <label for="username">choose username:</label> <input type="text" name="username" id="username" /> <span class="hint">minimum of 5 characters</span> <label for="email">your email address:</label> <input type="text" name="email" id="email" /> <label for="password">choose 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, have read terms of service , privacy policy.</label> <input type="submit" value="sign up" /> <input type="hidden" name="did_register" value="1" /> </form> </body> </html> 

what need fix?

  1. you should check error is:

    if (!$result_user) { die('mysql error: '.mysql_error()); } 
  2. call session_start() @ top of each of pages.

  3. and ensure session's values returned correctly:

    print_r($_session); 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -