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>