Register and Login error

Ask about a PHP problem here.
Post Reply
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Register and Login error

Post by JuiceYum »

I'm getting an error on my page to sign up. The link to my page is here: http://tumabackup.x10.mx/ (not sure if you can see it outside of Australia).

The errors are:

Warning: mysql_result() expects parameter 1 to be resource, boolean given in /home/tumaback/public_html/core/inc/users.inc.php on line 9

Warning: Cannot modify header information - headers already sent by (output started at /home/tumaback/public_html/core/inc/users.inc.php:9) in /home/tumaback/public_html/index.php on line 32

Here is my users.inc.php code:
<?php 

// Checks if the given user name exists in the database.
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;
	echo mysql_error();
}

// Checks if the given 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_name`, `user_password`) VALUES ('{$user}', '{$pass}')");
}

?>
And my init.inc.php
<?php 

session_start();

$exceptions = array('index','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', 'accountname', '*********');
mysql_select_db('tumaback_user_system');

$path = dirname(__FILE__);

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

?>
Edit: Also heres my index.php
<?php 

error_reporting(E_ALL);
ini_set('display_errors', 1); 

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[] = 'Passwords dont match!';
	}
	
	if (user_exists($_POST['username'])){
		$errors[] = 'Username already exists!';
	}
	
	if (empty($erros)){
		add_user($_POST['username'], $_POST['password']);
		
		$_SESSION['username'] = htmlentities($_POST['username']);
		
		header('Location: protected.php');
		die();
	}	
}

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>NewCo | Sign Up</title>

<link rel="shortcut icon" href="favicon.ico">

<link href='http://fonts.googleapis.com/css?family= ... cento+Sans' rel='stylesheet' type='text/css'>

<link href="/ext/style/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>

<div id="contact">
	<h1>Under Construction.</h1>
		<?php 
		
			if (empty($errors) === false){
				?>
				<ul>
					<?php
						
						
						foreach ($errors as $error){
							echo "<li>{$error}</li>";
						}
						
					?>
				</ul>
				<?php
			}
		
		?>
	<form action="/" method="post">
			<input type="text" name="username" id="username" placeholder="Username" />

			<input type="password" name="password" id="password" placeholder="Password" />

			<input type="password" name="repeat_password" id="repeat_password" placeholder="Re-Type Password">

			<input type="submit" value="Submit" />

	</form>
</div>

</body>
</html>
Please help!
Last edited by JuiceYum on Fri Feb 10, 2012 7:44 am, edited 3 times in total.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Register and Login error

Post by Temor »

The mysql_error(); will not tell you what is wrong if it is called after the return. Try putting it on the line before the return line.
Last edited by Temor on Thu Feb 09, 2012 11:27 am, edited 2 times in total.
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Re: Register and Login error

Post by JuiceYum »

I got this:

FUNCTION COUNT does not exist
Warning: mysql_result() expects parameter 1 to be resource, boolean given in /home/tumaback/public_html/core/inc/users.inc.php on line 9
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Register and Login error

Post by Temor »

JuiceYum wrote:I got this:

FUNCTION COUNT does not exist
Warning: mysql_result() expects parameter 1 to be resource, boolean given in /home/tumaback/public_html/core/inc/users.inc.php on line 9
That's because the function is defined like this : COUNT()

Remove the space between COUNT and (`user_id`)
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Re: Register and Login error

Post by JuiceYum »

Alrighty done but now it's changed to:


No database selected
Warning: mysql_result() expects parameter 1 to be resource, boolean given in /home/tumaback/public_html/core/inc/users.inc.php on line 9
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Register and Login error

Post by Temor »

JuiceYum wrote:Alrighty done but now it's changed to:


No database selected
Warning: mysql_result() expects parameter 1 to be resource, boolean given in /home/tumaback/public_html/core/inc/users.inc.php on line 9
That error is pretty self explanatory. You probably misspelled "tumaback_user_system" or something. Check for any typos.
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Re: Register and Login error

Post by JuiceYum »

I can't find any typos :/
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Re: Register and Login error

Post by JuiceYum »

Alright so when I deleted my database I still get the same arror so I'm making a new database with a different name.
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Re: Register and Login error

Post by JuiceYum »

So I remade the database and still nothing. I also checked all the typing for everything after making the new database.
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Re: Register and Login error

Post by JuiceYum »

I think the problem is the user doesn't have permission to access the database? Do you know how to change this?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login error

Post by jacek »

JuiceYum wrote:I think the problem is the user doesn't have permission to access the database? Do you know how to change this?
Is the name of your database definitely tumaback_user_system ?
Image
Post Reply