Page 1 of 1

register/login acount -- fetal errors

Posted: Sun Aug 26, 2012 10:11 pm
by tapiyuki
Dear all;
I worked very hard check more than 10 times and spend around a week already, but i couldn't find and solve the errors.
Can anyone help me ,please?
when i try to register I always got this error:
"Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\tapi\core\inc\user.inc.php on line 6 ".
user.inc.php
 
<?php
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; 
}	

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; 
}

function add_user($user, $pass){ 
	$user = mysql_real_escape_string(htmlentities($user)); 
	$pass = sha1($pass);
	mysql_query("INSERT INTO `users` (`user_name`, `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('Locaton: login.php'); 
		die(); 
	}
}


mysql_connect('127.0.0.1' , 'root' , '485849'); 
mysql_select_db('user_system'); 
$path = dirname(__FILE__);
include("{$path}/inc/user.inc.php");

?>
register.php


<?php
include("core/inc/user.inc.php");

$errors = array();

if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
	if (empty($_POST['username'])){ //check to see username is not empty.
		$errors[] = 'The username cannot be empty.';
	}

	if (empty($_POST['password']) || empty($_POST['repeat_password'])){
		$errors[] = 'The password and repeat_password cannot be empty.';
	}
	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 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
	</head>
	<body>
		<p>
			<?php
				if (empty($errors) === false)
				{
					?>
					<ul>
						<?php
						foreach ($errors as $errors) 
						{
							echo "<li>{$errors}</li>";
						}
					?>
					</ul>
					<?php
				}

			?>
		</p>
		<form action=""method="post">
			<p>
				<label for="username">username:</label>
				<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
			</p>
			<p>
				<label for="password">password:</label>
				<input type="password" name="password" id="password"/>
			</p>
			<p>
				<label for="repeat_password">repeat password:</label>
				<input type="password" name="repeat_password" id="repeat_password"/>
			</p>
			<p>
				<input type="submit" value="register"/>
			</P>
		</form>
	</body>
	</head>
</html>
login.php

<?php

include('core/init.inc.php');
$errors = array();
if (isset($_POST['username'], $_POST['password']))
{
	if (empty($_POST['username']))
	{
		$errors[] = 'The username cannot be empty.';
	}
	if (empty($_POST['password']))
	{
		$errors[] = 'The username cannot be empty.';
	}
	if (valid_credentials($_POST['username'], $_POST['password']) === false)
	{
		$errors[] = 'Username / Password incorrect.';
	}
	if (empty($errors))
	{
		$_SESSION['username'] = htmlentities($_POST['username']);
		header('Locaiton: protected.php');
		die();

	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
	</head>
	<body>
		<div>
			<?php 
			if (empty($errors) === false)
			{
			?>
				<ul>
					<?php
					foreach ($errors as $errors)
					{
						echo"<li>{$errors}</li>";
					}
					?>
				</ul>

			<?php	
			}else{
				echo 'Need an account ? <a href="register.php">Register here</a>';
			}
			?>
			
		<form action=""method="post">
			<p>
				<label for="username">username:</label>
				<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
			</p>
			<p>
				<label for="password">password:</label>
				<input type="password" name="password" id="password"/>
			</p>
			<p>
				<input type="submit" value="login"/>
			</P>
		</form>
	</body>
	</head>
</html>
protected.php

<?php include('core/init.inc.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
	</head>
	<body>
		<p>
			you are logged in as <?php echo $_SESSION['username'];  ?>
		</p>
		<p>
			<a> href="logout.php"> Logout ? </a>
		</p>
	</body>
</html	
logout.php

<?php

session_start();

$_SESSION = array();

session_destroy();

header('Location: protected.php');

?>

Re: register/login acount -- fetal errors

Posted: Mon Aug 27, 2012 11:16 pm
by jacek
That error usually means that your query is failing for some reason, if you add
echo mysql_error();
under the mysql_query() line it will usually tell you why. It's often a wrong column name or something simple.

Re: register/login acount -- fetal errors

Posted: Mon Aug 27, 2012 11:37 pm
by tapiyuki
I did as you suggested. It said "no database selected", but I already created a database and also follow exactly like the tutorials how to connect and select the database"Register.php" code. Please help !

Re: register/login acount -- fetal errors

Posted: Tue Aug 28, 2012 5:32 am
by Helx
Make sure "mysql_select_db('user_system')" in init.inc.php is actually pointing to a database.
For example, one of my website databases names is 'banning' (I'm publishing this because it has a prefix, but I wont tell you that for security and simplicity reasons. I dont want to confuse people :))

If you are pointing to a table in your database, you are doing it a tad wrong :)