User Account System - not working

Post here is you are having problems with any of the tutorials.
Post Reply
keystone
Posts: 10
Joined: Mon Aug 20, 2012 10:00 pm

User Account System - not working

Post 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.

[syntax=php]<?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>[/syntax]

Thanks for the help in advance : )
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Account System - not working

Post by Temor »

There are a few typos.
Line 7:
[syntax=php]if (isset($_POST['username'], $_POST['password'], $POST['check_password']))[/syntax]
you forgot an underscore in the last value.
Line 20:
[syntax=php] if ($_POST['passowrd'] !== $_POST['check_password'])[/syntax]
you spelled Password wrong.


Fixing those will solve your problems.
keystone
Posts: 10
Joined: Mon Aug 20, 2012 10:00 pm

Re: User Account System - not working

Post by keystone »

Thanks Temor that got me a good ways.

Do you see anything wrong here?

[syntax=php]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;

}[/syntax]

it gives an error concerning the argument of the "mysql_result()"
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Account System - not working

Post by Temor »

That error means something is wrong with the SQL statement.
Adding [syntax=php] echo mysql_error();[/syntax]
underneath the mysql_query(); will provide details on the error.
keystone
Posts: 10
Joined: Mon Aug 20, 2012 10:00 pm

Re: User Account System - not working

Post 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:

[syntax=php]mysql_connect('database_host', 'data_username, 'data_pass');
mysql_select_db('data_name');[/syntax]

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-)
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Account System - not working

Post 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.

[syntax=php]mysql_connect('database_host', 'data_username, 'data_pass');[/syntax]

you forgot a quote after data_username.
Post Reply