Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing mysql_query in PHP to show the user name is not working
    primarykey
    data
    text
    <p>I am currently trying to create a registration form, and I have the form itself working and people can create user's in my database, but when they sign up and it redirects them to the <code>admin.php</code>.</p> <p>The name they used to create an account doesn't show up, down by row user name. It should say "Welcome, <code>user_name</code>, you are now logged in!"</p> <p>I just can't get the name to show up but everything else works!</p> <blockquote> <p>Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\path\to\admin.php on line 25<br> Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\path\to\login.php on line 36</p> </blockquote> <p><strong>Admin:</strong></p> <pre><code>&lt;?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']; ?&gt; &lt;!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;link rel="stylesheet" type="text/css" href="css/reset.css" /&gt; &lt;link rel="stylesheet" type="text/css" href="css/format.css" /&gt; &lt;title&gt;Schell Shock Design's Portfolio&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="login"&gt; &lt;?php include('login.php'); ?&gt; &lt;/div&gt; &lt;div id="utilities"&gt; &lt;?php include('utilities.php'); ?&gt; &lt;/div&gt; &lt;div id="container"&gt; &lt;header&gt; &lt;?php include('header.php'); ?&gt; &lt;/header&gt; &lt;div id="slider"&gt; &lt;?php include('slider.php'); ?&gt; &lt;/div&gt; &lt;div id="content"&gt; &lt;?php include('content.php'); ?&gt; &lt;/div&gt; &lt;div id="bottomcontent"&gt; &lt;?php include('bottomcontent.php'); ?&gt; &lt;/div&gt; &lt;div id="footer"&gt; &lt;?php include('footer.php'); ?&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Login:</strong></p> <pre><code>&lt;?php //show an error if there is a problem with the login if($error == true){ ?&gt; &lt;div class="error"&gt; Sorry, Your username and password are incorrect. Try again. &lt;/div&gt; &lt;?php } //end if error ?&gt; &lt;?php //show the form only if NOT logged in if( !$_SESSION['logged_in'] ){ ?&gt; &lt;div class="form1"&gt; &lt;form action="?action=" method="post"&gt; &lt;label for="username"&gt;Username:&lt;/label&gt; &lt;input type="text" name="username" id="username" /&gt; &lt;label for="password"&gt;Password&lt;/label&gt; &lt;input type="password" name="password" id="password" /&gt; &lt;input type="submit" value="Log in" /&gt; &lt;input type="hidden" name="did_login" value="1" /&gt; &lt;/form&gt; &lt;?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 ); ?&gt; &lt;div id="loggedin"&gt; &lt;a href="?action=logout"&gt;Log Out&lt;/a&gt; &lt;?php //show a welcome message if they logged in successfully echo 'Welcome '.$row_user['name'].', You are now logged in!'; ?&gt; &lt;?php } ?&gt; &lt;/div&gt; </code></pre> <p><strong>Registration</strong></p> <pre><code>&lt;?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. &lt;br /&gt;'; } //repeated password does not match if( $password != $repassword ){ $valid = false; $msg .= 'The passwords provided do not match. &lt;br /&gt;'; } //make sure the username and password are at least 5 characters long, than check the database if( strlen($username) &gt;= 5 AND strlen($password) &gt;= 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. &lt;br /&gt;'; } }else{ $valid = false; $msg .= 'Username and Password must be at least 5 characters long. &lt;br /&gt;'; } //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? &lt;br /&gt;'; } }else{ //invalid email $valid = false; $msg .= 'Please provide a valid email address. &lt;br /&gt;'; } //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 ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Sign up for an account&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;?php if( isset($msg) ){ echo $msg; } ?&gt; &lt;form action="registration.php" method="post"&gt; &lt;label for="username"&gt;Choose a Username:&lt;/label&gt; &lt;input type="text" name="username" id="username" /&gt; &lt;span class="hint"&gt;Minimum of five characters&lt;/span&gt; &lt;label for="email"&gt;Your Email Address:&lt;/label&gt; &lt;input type="text" name="email" id="email" /&gt; &lt;label for="password"&gt;Choose a Password:&lt;/label&gt; &lt;input type="password" name="password" id="password" /&gt; &lt;span class="hint"&gt;Minimum of 5 characters&lt;/span&gt; &lt;label for="repassword"&gt;Repeat Password:&lt;/label&gt; &lt;input type="password" name="repassword" id="repassword" /&gt; &lt;input type="checkbox" name="policy" id="policy" value="1" /&gt; &lt;label for="policy"&gt;Yes, I have read the Terms of Service and Privacy Policy.&lt;/label&gt; &lt;input type="submit" value="Sign up" /&gt; &lt;input type="hidden" name="did_register" value="1" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>What do I need to fix?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload