<?php
if($_POST){
$password=$_POST["password"];
$confirm=$_POST["confirm"];
if($password != $confirm){ ?>
<span style="color:red">Error: Passwords do not match!</span>
<?php } else {
$dbhost ="localhost";
$dbuser ="root";
$dbpass ="";
$dbname ="users";
require_once"/wamp/scripts/config.inc.php";
$conn =mysql_connect($dbhost,$dbuser,$dbpass)
or die ("Error connecting to mysql");
echo mysql_error ();
mysql_select_db($dbname);
$query=sprintf("SELECT *FROM users WHERE username ='%s'",
mysql_real_escape_string($_POST['username']));
$result=mysql_query($query);
if($result){ ?>
<span style="color:red">Error:That username is taken.</span>
<?php } else {
$query= sprintf("INSERT INTO users(username,password) VALUES ('%s','%s');",
mysql_real_escape_string($_POST["username"]),
mysql_real_escape_string(md5($password)));
mysql_query($query);
?>
<span style="color:green">Congratulations, you have registered successfully!</span>
<?php
}
}
}
?>
<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!" />
</form>
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.