if($password != $confirm){ ?>
<span style="color:red">Error: Passwords do not match!</span>
<?php   } else {
					$dbhost ="localhost";
					$dbuser ="root";
					$dbpass ="";
					$dbname ="users";
		$conn =mysql_connect($dbhost,$dbuser,$dbpass)
				or die ("Error connecting to mysql");
				echo mysql_error ();
		$query=sprintf("SELECT *FROM users WHERE username ='%s'",
		if($result){ ?>
<span style="color:red">Error:That username is taken.</span>
<?php		}  else {
				$query= sprintf("INSERT INTO users(username,password) VALUES ('%s','%s');",
<span style="color:green">Congratulations, you have registered successfully!</span>
<form method="post" action="Registration.php">
Username: <Input type="text" name="username" /><br />
Password: <Input type="password" name="password" /><br />
Confirm Password: <Input type="Password" name="confirm" /><br />
<Input type="submit" value"Register!" />

Whenever I run the code I can enter a username and password that don't exist in my database yet it ALWAYS returns the error "Error:That username is taken" and doesn't go any further.

I am teaching myself php and have been following this from a tutorial. It has changed from the tutorial as in the comments the problem I was having before came up many times and he recoded it to the above. I have been doing this for hours now but to no avail. Please help and point out where i'm going wrong in plain english.

Many thanks.

7 Years
Discussion Span
Last Post by pritaeas

I'm afraid I don't.
If I put a ; after it I get a Parse error a couple of lines down.


I am really struggling here. I change the line to read

if(!$result) {?>

It then enters the code into the database, yet I can enter the same username and it will still enter it into the database even though its already in. I have read your link above and I think I understand (though probably i'm not).

if (mysql_num_rows($result) > 0) {

Change as above and also check whether the query is properly defined. To confirm echo all the values ($password, $confirm) to see the value. And more you can do open your phpadmin and run the sql query from your editor and see whether it returning rows.


Thank you very much.

Why does it always seem so simple when some-one points out the obvious!! (well to you anyway)

Thanks Again.


Why does it always seem so simple when some-one points out the obvious!! (well to you anyway).

More importantly, do you now know WHY you have to do it like that ?


Is is it this......

if (mysql_num_rows($result) > 0) {

Line 1 stores the result of the query in $result.
Line 2 counts how many rows are in the query and if there is more than 0 then it cant use the details you entered.

Thats the way i'm thinking anyway.


True, but what I attempting to show you is that mysql_query will return false, only if the query fails with an error. So if the query would not return any results, it still returns a resource, and not false.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.