mckooter Posted November 10, 2006 Share Posted November 10, 2006 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 helpchris Quote Link to comment Share on other sites More sharing options...
soycharliente Posted November 10, 2006 Share Posted November 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
mckooter Posted November 10, 2006 Author Share Posted November 10, 2006 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 Quote Link to comment Share on other sites More sharing options...
soycharliente Posted November 10, 2006 Share Posted November 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
mckooter Posted November 11, 2006 Author Share Posted November 11, 2006 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 namewhat 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 Quote Link to comment Share on other sites More sharing options...
joshi_v Posted November 11, 2006 Share Posted November 11, 2006 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! Quote Link to comment Share on other sites More sharing options...
Eugene Posted November 11, 2006 Share Posted November 11, 2006 Make your username field a primary key in the database. This won't allow you to enter the same 1 twice Quote Link to comment Share on other sites More sharing options...
Nhoj Posted November 12, 2006 Share Posted November 12, 2006 [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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.