Database issue... - PHP Tutorial: Register and Login

Post here is you are having problems with any of the tutorials.
Post Reply
garret27
Posts: 4
Joined: Fri Jul 06, 2012 12:50 am

Database issue... - PHP Tutorial: Register and Login

Post by garret27 »

Just joined the forum.
I am in part 4 of the video tutorial but I've seem to run into a problem with the database. Once I finished part 3 or so, I tried to create an account, but to no avail. My database isn't receiving any data from the register forms (i think.) I try to browse whats in it, but there is nothing. So I must have mistyped something in my tunnel vision....

Also I get a "Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\project01\core\inc\user.inc.php on line 9" occasionally, but that is when I click submit without any data in the boxes. I am not sure if it has anything to do with the database not receiving any info.

I have also uploaded a picture of my database, just in case I entered something wrong when creating the tables.

user.inc.php
 <?php

// Checks to see if the username exists.
function user_exists($user){
	$user = mysql_real_escape_string($user);

	$total = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'user_name' = '{$user}'");

	return (mysql_result($total, 0) == '1') ? true : false; 
}

// Checks if the Username and Password combination is valid.
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = sha1($pass);
	
	$total = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'user_name' = '{$user}' AND 'user_password' = '{$pass}'");
	
	return (mysql_result($total, 0) == '1') ? true : false;
}

//  Adds a user to the database. 
function add_user($user, $pass){
	$user = mysql_real_escape_string(htmlentities($user));
	$pass = sha1($pass);
	
	mysql_query("INSERT INTO 'users' ('user_password') VALUES ('{$user}', '{$pass}')");
}

?> 
init.inc.php
<?php

session_start();

$exceptions = array('register', 'login');

$page = substr(end(explode('/', $_SERVER['SCRIPT_NAME'])), 0, -4);

if (in_array($page, $exceptions) === false) {
	if (isset($_SESSION['username']) === false){
		header('Location: login.php');
		die();
	
	}
}

mysql_connect('localhost', 'root', '');
mysql_select_db('gamesaves');

$path = dirname(__FILE__);

include("{$path}/inc/user.inc.php");

?>
protected.php
<?php include('core/init.inc.php'); ?>
<!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>Login in as </title>
</head>

<body>
<div align="center">
  <p>Logged in as: </p>
  <p>Redirect in a few seconds</p>
</div>
</body>
</html>

register.php
<?php

include('core/init.inc.php');

$errors = array();

if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
	if (empty($_POST['username'])){
		$errors[] = 'The username cannot be empty.';
	}
	
	if (empty($_POST['password']) || empty($_POST['repeat_password'])){
		$errors[] = 'The password cannot be empty.';
	}
	
	if ($_POST['password'] !== $_POST['repeat_password']){
		$errors[] = 'Password verification failed.';
	}
	
	if (user_exists($_POST['username'])){
		$errors[] = 'The username you entered is already taken.';
	}
	
	if (empty($errors)){
		add_user($_POST['username'], $_POST['password']);
		
		$_SESSION['username'] = htmlentities($_POST['username']);
		
		header('Location: protected.php');
		die();
		
	}
		
	
}

?>
<!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" />
<link rel="stylesheet" href="stylesheets/style.css">
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<script type="text/javascript" src="scripts/animatedcollapse.js"></script>
<script type="text/javascript">

animatedcollapse.addDiv('login', 'fade=1,height=80px')


animatedcollapse.ontoggle=function($, divobj, state){ //fires each time a DIV is expanded/contracted
	//$: Access to jQuery
	//divobj: DOM reference to DIV being expanded/ collapsed. Use "divobj.id" to get its ID
	//state: "block" or "none", depending on state
}

animatedcollapse.init()

</script>


<title>Online Gamesaves - Alpha</title>
</head>

<body onLoad="javascript:hideDiv()">
<div class="wrapper" id="wrapper">
  <div class="header" id="header">
  
  	<div class="logo" id="logo"><a href="index.php"><a href="index.php"><img src="images/logo.jpg" alt="Online Gamesaves" width="500" height="167" /></a></div>
    
    <div id="login" style="display:none"><a href="javascript:animatedcollapse.hide('login')">Exit</a> - Login<br />
    User: 
      <label>
      <input name="user" type="text" id="user" size="15" />
      Password: 
      <input name="password" type="text" id="password" size="15" />
      <br />
      <input type="submit" name="Login" id="Login" value="Login" />
      </label>
    </div>
    
    
    <div class="userdata" id="userdata"><a href="javascript:animatedcollapse.show('login')">Sign In</a> - Register</div>
<div class="search" id="search">Search<br />
            <input name="Search" type="text" id="Search" size="14" />
            <select name="searchrefine">
              <option>Users</option>
              <option>Games</option>
              <option>Systems</option>
              <option>Gamesave Editors</option>
            </select><input name="Go" type="button" value="Go" />
    </div>
  </div>
  <div class="menu" id="menu">Home -
    How it works! -
    Personal Backups - 
    Gamesaves Editors -
  Help</div>
  <div class="mainbody" id="mainbody">
  	<div class="pageheader" id="pageheader"> 
  	  <h1 align="center">Register</h1>  
  	</div>
    <div class="bodycontent" id="bodycontent">
     <form action="" method="post">
      <table width="60%" border="0" align="center">
        <tr>
          <td><div align="center">Username:<br />
            <label>
              <input type="text" name="username" id="username" />
            </label>
          </div></td>
          <td><div align="center">Password<br />
            <label>
              <input type="password" name="password" id="password" />
            </label>
          </div></td>
        </tr>
        <tr>
          <td colspan="2"><div align="center">Repeat Password<br />
            <input type="password" name="repeat_password" id="repeat_password" />
          </div></td>
          </tr>
        <tr>
          <td colspan="2"><div align="center">
            <label>
            <input type="submit" name="button" id="button" value="Register" />
            </label>
          </div></td>
          </tr>
      </table>
      <p align="center">
	  <?php
			if (empty($errors)===false){
	  ?>
		<ul>
	<?php
	foreach ($errors as $error){
		echo "<li>{$error}</li>";
			}
			?>
</ul>
<?php
}
?></p>
     </form>
    </div>
  </div>
</div>
</body>
</html>
Attachments
My database.
My database.
database.gif (70.23 KiB) Viewed 3423 times
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Database issue... - PHP Tutorial: Register and Login

Post by Temor »

This error:
Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\project01\core\inc\user.inc.php on line 9
usually means that the SQL query failed, because if it does, it returns false ( which is a boolean ).
Adding
echo mysql_error();
under the query that is failing will tell you what is wrong.

In this case I can tell you it's because of invalid use of apostrophes.
Example:
SELECT COUNT('user_id') FROM 'users' WHERE 'user_name' 
Should be
SELECT COUNT(`user_id`) FROM `users` WHERE `user_name`
Apostrophes should surround a string only. Table and column names should always be surrounded by backticks ( ` )

You have made this mistake in every query, so once you fix that your problems should be solved :)
garret27
Posts: 4
Joined: Fri Jul 06, 2012 12:50 am

Re: Database issue... - PHP Tutorial: Register and Login

Post by garret27 »

Thanks man!

Alternatively I found a previous thread on this forum explaining the same thing about the " ' " and " ` " .
From the tutorial it seemed liked they are the same thing.

So again, thanks! :D
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Database issue... - PHP Tutorial: Register and Login

Post by Temor »

They do look the same. I suggest watching in full HD and fullscreen. It's a lot easier to spot small things like that in high resolution :P
garret27
Posts: 4
Joined: Fri Jul 06, 2012 12:50 am

Re: Database issue... - PHP Tutorial: Register and Login

Post by garret27 »

I did watch it in hd. It helped alot. I've also created my registration system now working on adding a Captcha.
Post Reply