Jump to content

check if username already in database...


mckooter

Recommended Posts

okay im working on a script that takes users information and stores it to database, a very simple thing, the problem is it allows users (from subdreamer cms) to sign up multiple times, (this is different than the usersystem built into the cms)

what i need to be able to do is search the database (all in one table) for a username if it exsists than display one page and if it doesent display the form.

I have a global variable that has the username, and when the person fills out the form it automatically stores it to the database so now i need to scan the database for it,

im sure its a simple select query but how do i get php to give me a simple yes or no from a select query? can i select the name and then check to see if the result is !=0 or so, im not sure and cant seem to get it to work, a bit of guidance and ill surely figure it out and apply it,

thanks for any help

chris
Link to comment
Share on other sites

When you set up your database table, set the Username as the Primary Key. I don't know SQL syntax (you can probably just search for it, it will make you better, lol), but what that does is automatically evaluate each Username against all others whenever it tries to create an entry. If it does exist, it will fail, so just have a boolean to see if it failed or not. If boolean is true, include one file, if not, include another.

I have this code in use:
[code]// Make a safe query
$query = sprintf("INSERT INTO Users(Username, Password, Email, Status) VALUES(%s, %s, %s, %s)",
quote_smart($uname),
quote_smart($pwd),
quote_smart($mail),
quote_smart("Unapproved"));
$result = mysql_query($query);[/code]
And later on:
[code]<?php
if ($result == 1) {
echo "Thank you for registering.";
} else {
echo "There was a problem creating your account.";
}
?>[/code]

Hope that helps.
Link to comment
Share on other sites

yes, i figured that would be one option, but rather than wait till someone fills out the form to tell them that the username is already taken i want the form to know when its loaded.

in subdreamer the username is stored in a global variable $userinfo['username']

what i want to be able to do is something where it can check if the above variable is in the database before even starting and generate a variable that i can run a simple if statement on, the variable would most likely be a 1 or 0,  or it could even be (blank or (the username))

hopefully that helps to what im looking for
Link to comment
Share on other sites

It seems like you're trying to write code for something that is already handled for you. Maybe I'm misunderstanding. Sorry if I am. You want to check to see if it exists before they even submit?

That could be done using AJAX, so that it runs a query even while the user is typing in his information. As soon as he clicks off the input field, it would check and return something. Though I don't know how to AJAXify anything, I assume it would just be the same function but called before they click submit.
Link to comment
Share on other sites

sorry, i think im being confusing, what im trying to say is the username is already in a variable before the user even enters the page

$userinfo['username'] is the variable that has the username in it (before they even click on the page) they have to be logged into the site to be able to even view the page im talking about therefore i already have their name

what im trying to do is search the database to see if their name is already in it, if it is then just display one thing, if not display another
Link to comment
Share on other sites


Is this is what you want?

[code]
<?php
$sq="select * from usertable where name=$userinfo['username'] ";
$qry=mysql_query($sq) or die (mysql_error());
if(mysql_num_rows($qry)>0)//This means user is already registered .
{
  // Code goes here!
}
else
{
  //code
}
?>
[/code]

Hope this helps you!
Link to comment
Share on other sites

[code]
<?php
$found = mysql_result(mysql_query('SELECT count(0) FROM `users_table` WHERE `uName` = "'.$userinfo['username'].'" LIMIT 1'), 0, 0);

if ($found == 1) {
echo "user found";
} else {
echo "user not found";
}
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.