/    Sign up×
Community /Pin to ProfileBookmark

Registration: Checking for existing Username in Database

I’m new to PHP and this is driving me nuts… for some reason, even if the database is empty, whatever username I enter, it tells me the username is already in the database. I also have it checking to see if the same password was written twice, but that is working fine.

Here’s the HTML form:

[code=html]<form action=”registerconfirm.php” method=”POST”>
Username: <input type=”text” name=”username” maxlength=”16″ /><br>
NYU E-mail: <input type=”text” name=”email” /><br>
Password: <input type=”text” name=”pword1″><br>
Password (again): <input type=”text” name=”pword2″><br>
<input type=”submit” value=”Register”>
</form>[/code]

Here’s registerconfirm.php (where it checks if same password was typed twice, and if the username exists already in database):

[code=php]<?php
include(“components/scripts.php”);

$username = $_POST[‘username’];
$email = $_POST[’email’];
$pword1 = $_POST[‘pword1’];
$pword2 = $_POST[‘pword2’];

if ($pword1 != $pword2) { // check if passwords match
echo “Passwords do not match. Please re-enter.”;
} else {
dbconnect(); //calls function from “components/scripts.php” to connect to database
$query = “SELECT * FROM users WHERE username=’$username'”; // check if username already exists
$result = mysql_query($query);
if ($result) {
echo “Our database shows this username has already been registered. Please choose another username.”;
} else {
echo “Good”;
}
mysql_close();
}

?>[/code]

Anyone know what’s wrong with this code that it’s giving me the “our database shows the username has already been registered” message every time?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@PineSolPirateJan 04.2007 — Your [B]if($result)[/B] is only going to be false if there is something wrong with the query, like a misplaced quotation or syntax error.

Try replacing that with [B]if(0 != mysql_num_rows($result))[/B]. That will check how many rows got returned in the query vs. whether the query executed.
Copy linkTweet thisAlerts:
@NEONecd999authorJan 04.2007 — Your solution worked beautifully. Thank you!
Copy linkTweet thisAlerts:
@NightShift58Jan 04.2007 — While you're at it, change:[code=php] $query = "SELECT * FROM users WHERE username='$username'"; // check if username already[/code]to:[code=php] $query = "SELECT * FROM users WHERE username='$username' LIMIT 1"; // check if username already[/code]
Copy linkTweet thisAlerts:
@NEONecd999authorJan 04.2007 — Thanks NightShift58, I'm guessing that makes it stop searching after it found one instance, making it faster?

One more thing-- I want to be able to detect if a user inputs as their username any special characters, or anything other than letters and numbers. How do I do this?

Thanks again.
Copy linkTweet thisAlerts:
@NightShift58Jan 04.2007 — I think you have two options, both of which would involve regular expression.

You could use javascript and validate on the spot, before the form is submitted (actually onsubmit) or you could post-process in the following page and use PHP preg's to do the same.

Hopefully Bokeh or NogDog will look at this thread as well - and they'll be able to provide a working RegEx.
Copy linkTweet thisAlerts:
@MrCoderJan 05.2007 — [code=php]
$username = "abcdef";

if(preg_match("/([^a-z0-9]+)/i", $username) != 0)
{
echo "Invalid!";
} else {
echo "Ok!";
}
[/code]


This will echo "Invalid!" if $username contains anything other then a-z 0-9.

A space in $username will also flag as invalid.

The above is also case insensitive.
Copy linkTweet thisAlerts:
@NEONecd999authorJan 05.2007 — awesome, thanks.
×

Success!

Help @NEONecd999 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...