Page 1 of 1

User Account System - not working

Posted: Mon Aug 20, 2012 10:09 pm
by keystone
First off these are awesome tutorials.

Some reason my register.php is only saving the username and reloading that page with its value saved. I can't figure out why none of the errors come up.
<?php

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

$errors = array();

if (isset($_POST['username'], $_POST['password'], $POST['check_password']))

{
	if (empty($_POST['username']))
	{
	    $errors[] = 'You need a username.';
	}

	if (empty($_POST['password']) || empty($_POST['check_password']))
	{
	    $errors[] = 'You need a password.';
	}

	if ($_POST['passowrd'] !== $_POST['check_password'])
	{
	    $errors[] = 'The passwords do not match.';
	}

	if(user_exists($_POST['username']))

	{
	    $errors[] = 'The username is already taken.';	 
	}

	if (empty($errors))

	{
	    add_user($_POST['username'], $_POST['password']);

	    $_SESSION['username'] = htmlentities($_POST['username']);

	    header('Location: home.php');
	    die();
	}
	
}

?>

<html>

<head>
</head>

<body>
<p>
<?php
// display error messages

if (empty($errors) === false)
{
?>

	<ul>
<?php
	foreach ($errors as $error)

	{
	    echo"</li>($error)</li>";
	}
?>
	</ul>
<?php
}
?>
</p>
<div>

	<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="check_password">Check Password:</label>
	<input type="password" name="check_password" id="check_password">
     </p>
	
     <p>
	<input type="submit" value="Submit" />
	</form>
     </p>
</div>


</body>
</html>
Thanks for the help in advance : )

Re: User Account System - not working

Posted: Mon Aug 20, 2012 10:25 pm
by Temor
There are a few typos.
Line 7:
if (isset($_POST['username'], $_POST['password'], $POST['check_password']))
you forgot an underscore in the last value.
Line 20:
   if ($_POST['passowrd'] !== $_POST['check_password'])
you spelled Password wrong.


Fixing those will solve your problems.

Re: User Account System - not working

Posted: Mon Aug 20, 2012 10:48 pm
by keystone
Thanks Temor that got me a good ways.

Do you see anything wrong here?
function user_exists($user)

{
	$user = mysql_real_escape_string($user);

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

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

}
it gives an error concerning the argument of the "mysql_result()"

Re: User Account System - not working

Posted: Mon Aug 20, 2012 11:50 pm
by Temor
That error means something is wrong with the SQL statement.
Adding
 echo mysql_error();
underneath the mysql_query(); will provide details on the error.

Re: User Account System - not working

Posted: Tue Aug 21, 2012 12:22 am
by keystone
Thanks again Temor that helped me figure out that it wasn't connecting to my database. I had to change the code a bit since I'm not connecting to a local db.

Instead of the mysql_connect(127.0.0.1...

I'm using:
mysql_connect('database_host', 'data_username, 'data_pass');
mysql_select_db('data_name');
but now it's not posting anything to my database, I have no idea where to start looking... oh looking through this forum I guess queries have to have tilde `

I'm used to not putting anything around my query. Do you know if it's better to have ` instead of nothing?

Works now 8-)

Re: User Account System - not working

Posted: Tue Aug 21, 2012 11:58 am
by Temor
I always use backticks ( ` ) around table and column names. It adds to the readability of the code. I'm not sure if it makes any other difference though.
mysql_connect('database_host', 'data_username, 'data_pass');
you forgot a quote after data_username.