Hey. i have another problem now....ive created a user register form now aswell. i can add to the database fine but when it checks if a user is already taken, it constantly shows this:
Sorry, there the username Dave is already taken.
Try again
this is when ive entered the name daniel cos this is in my database. so it should say daniel not dave.
I can add a totally new user that does not have a username already stored in the database fine thats not a prob.
Could you help please????
register.php
<?php
$host = "localhost"; //login to the server
$username = "Dave"; //username login
$password = "ozxoka"; //password login
$db_name = "logintest"; //login test and register
$tbl_name = "members"; //members tabel
// connect to the mysql server
$link = mysql_connect($host, $username, $password)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($db_name)
or die ("Could not select database because ".mysql_error());
// check if the username is taken
$check = "select id from $tbl_name where username = '".$_POST['username']."';";
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=userregistration.php>Try again</a>";
exit;
}
else {
// insert the data
$insert = mysql_query("insert into $tbl_name values ('NULL', '".$_POST['username']."', '".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());
// print a success message
echo "Your user account has been created!<br>";
echo "Now you can <a href=login.php>log in</a>";
}
?>
userregistration.php
<html><head>
<title>User Registration</title>
</head><body>
<form action="register.php" method="post">
Enter a Username: <input type="text" name="username" size="20"><br>
Enter a Password: <input type="password" name="password" size="20"><br>
<input type="submit" value="Sign Up">
</form>
</body></html>
registration page
Moderator: General Moderators
Re: registration page
Hi,
Please always include code in code tags or php tags as it makes it easier to read.
You should echo your variable, as I have highlighted in red, before the if statement to see what it actually equals. This should help you debug it.
If $num_rows (highlighted in red) does equal zero when it is supposed to then you know the code above worked and the problem lies with the if statement. Can I suggest changing the if statement to the following to see if it makes a difference: (I added " marks on either side of the 0)
Hope this helps
Kind Regards
AMCH
Please always include code in code tags or php tags as it makes it easier to read.
You should echo your variable, as I have highlighted in red, before the if statement to see what it actually equals. This should help you debug it.
Code: Select all
$check = "select id from $tbl_name where username = '".$_POST['username']."';";
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
[b][color=#FF0000]echo "$num_rows";[/color][/b]
if ($num_rows != 0) {
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=userregistration.php>Try again</a>";
exit;
}Code: Select all
if ($num_rows != [color=#00BF40][b]"[/b][/color]0[color=#00BF40][b]"[/b][/color]) {
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=userregistration.php>Try again</a>";
exit;
}Kind Regards
AMCH
Re: registration page
Hello ive changed the code as you said but no luck. still reading the same from the database.
Re: registration page
hi i'm also new to php, try to put a session
Re: registration page
Check the database to see if daniel has been added. The reason it keeps showing the error: "Sorry, there the username Dave is already taken." is because you are using the wrong variable.
Change
to
That will at least show you the correct username. Otherwise, you are displaying the database username.
Change
Code: Select all
if ($num_rows != 0) {
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=userregistration.php>Try again</a>";
exit;
}Code: Select all
if ($num_rows != 0) {
echo "Sorry, there the username $_POST['username'] is already taken.<br>";
echo "<a href=userregistration.php>Try again</a>";
exit;
}