Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,586 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,079 people online right now. Registration is fast and FREE... Join Now!




Checking if user exists or not

2 Pages V  1 2 >  
Reply to this topicStart new topic

Checking if user exists or not

brainy_creature
12 Sep, 2007 - 06:23 AM
Post #1

D.I.C Head
**

Joined: 7 Aug, 2006
Posts: 174



Thanked: 1 times
My Contributions
CODE
<?php
$gender=$_POST["gen"];
$user=$_POST[username];
$check="SELECT username FROM first where username=$user";
$finally=mysql_query($check);
$query="INSERT INTO first(username,password,fname,lname,gender) VALUES('$_POST[username]','$_POST[password]','$_POST[fname]','$_POST[lname]','$gender')";
$result=mysql_query($query);
mysql_close($con);
echo "Registration successful!</br> username:$_POST[username]<br>first name:$_POST[fname]<br> last name:$_POST[lname]";
?>


please tell me some function i could use here to check if the username already exist in the db


[mod edit] Added code tags. mad.gif
User is offlineProfile CardPM
+Quote Post

snoj
RE: Checking If User Exists Or Not
12 Sep, 2007 - 06:40 AM
Post #2

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 7 times
Dream Kudos: 700
My Contributions
You know, for a user who's been here a year and has over 100 posts, you'd think you'd get it that you need to use code tags.

As for your function. What does one do when they want to check if a given variable has a given value?

I'll give you a hint, it's not a function, its logic!
User is offlineProfile CardPM
+Quote Post

brainy_creature
RE: Checking If User Exists Or Not
12 Sep, 2007 - 07:09 AM
Post #3

D.I.C Head
**

Joined: 7 Aug, 2006
Posts: 174



Thanked: 1 times
My Contributions
you have been really rude all this while
its very suprising how easy everything seems when you know them perfectly well and how difficult things get at times when you are a beginner!!
i tried
CODE
foreach( username as $user)
if(mysql_num_rows($check)==1)
echo "the username already exist";
else
{
$query="INSERT (username.....


and it didnt work out so i wanted some help.. i went to php.net but didnt know wat function to search for tried a little but didnt work
and then i thought maybe i can get some help here!!
but useless... am never coming back to this sick place!
thanks for all ur help!!!

[mod edit] How many times must I edit your posts before you understand to add code tags?
User is offlineProfile CardPM
+Quote Post

snoj
RE: Checking If User Exists Or Not
12 Sep, 2007 - 07:40 AM
Post #4

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 7 times
Dream Kudos: 700
My Contributions
Well brainy, I'm sorry to see you leave and that my pointer/hint wasn't good enough.

However, from reading the code you provided, it would appear that you know enough to write PHP, but not "brainy" enough to actually read the documentation on the functions you're using. I know this because of the code you've show me. I know that mysql_num_rows() takes a variable that is returned by mysql_query(). From the code you've show, you were feeding it the query string.

While I know this without looking the documentation because of my years of experience, I knew in my early days to 1) ask questions the smart way and 2) make sure I've read everything and searched everywhere I could before asking questions. You have done neither.
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: Checking If User Exists Or Not
12 Sep, 2007 - 07:40 AM
Post #5

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
you can do a mysql_num_rows on the query and check whether it is more then 0 instead of whether it equals 1. This just might work. Sometimes when you put in "$variable" it doesn't replace it with a value try "{$variable}"

This post has been edited by ap0c0lyps3: 12 Sep, 2007 - 07:42 AM
User is offlineProfile CardPM
+Quote Post

snoj
RE: Checking If User Exists Or Not
12 Sep, 2007 - 09:36 AM
Post #6

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 7 times
Dream Kudos: 700
My Contributions
QUOTE
Sometimes when you put in "$variable" it doesn't replace it with a value try "{$variable}"

That has to do with how strings are parsed.

CODE

$canIHas = 'Can I has';
$string = '{$canIHas} cheezebuger?'; //Would be seen as "{$canIHas} cheezeburger?"
$string = "{$canIHas} cheezeburger"; //Would be seen as "Can I has cheezeburger?"


Note the difference, the " and '. The double quote (") allows you to place variables in the string and have their values placed in said string. The single quote mark (') is a literal string, what is put in there is what is seen.

Would you like to know more?
User is offlineProfile CardPM
+Quote Post

brainy_creature
RE: Checking If User Exists Or Not
13 Sep, 2007 - 07:44 AM
Post #7

D.I.C Head
**

Joined: 7 Aug, 2006
Posts: 174



Thanked: 1 times
My Contributions
alrite am very sorry ill take care!!
i used mysql_num_rows($query) so many times and also tried few other functions which i knew but some stupid erroe like is confusing me

error:Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/www/bakwas.awardspace.com/register.php on line 7



User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Checking If User Exists Or Not
13 Sep, 2007 - 12:11 PM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 39 times
Dream Kudos: 25
My Contributions
Given your code above, it is telling you that the variable you have supplied to the function ($check) is not of the proper type. that variable appears to be the actual text of your initial sql query, while the function is expecting the value returned by the execution of that query.
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: Checking If User Exists Or Not
14 Sep, 2007 - 03:37 AM
Post #9

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
try echoing your query like echo $check." ".$query and see whether it queries correctly

This post has been edited by ap0c0lyps3: 14 Sep, 2007 - 12:13 PM
User is offlineProfile CardPM
+Quote Post

brainy_creature
RE: Checking If User Exists Or Not
14 Sep, 2007 - 05:41 AM
Post #10

D.I.C Head
**

Joined: 7 Aug, 2006
Posts: 174



Thanked: 1 times
My Contributions
thanks for that help... hopefully it'll work now!!
thanks smile.gif
User is offlineProfile CardPM
+Quote Post

antiturncoat
RE: Checking If User Exists Or Not
17 Sep, 2007 - 06:17 PM
Post #11

D.I.C Head
**

Joined: 8 Aug, 2007
Posts: 98


My Contributions
heres a login example:


CODE

<?php

session_start();
include 'connect.php';

$username=$_POST['username'];
$password=$_POST['password'];

    if(isset($_POST['login']))
    {
        if( empty($username) || empty($password) )
        {
            include 'Login.php';
            ?>
                <script language="javascript1.2">
                    alert("Empty Fields. Fill up all the fields");
                </script>
            <?
         }
    
      else
      {
          //check if the username and password is in the database
          $query = "SELECT username,password FROM accounts WHERE username = '$username' and password= '$password' ";
          $result = mysql_query($query) or die(mysql_error());
          
              if(mysql_num_rows($result)==0)
            {
                //if either username name or password doesn't exist
                ?>
                    <script language="javascript1.2">
                        temp=confirm("Invalid username or password or not yet registerd. Do you want to register?");
                            if(temp)
                                window.location = "Register.php"
                               else
                                window.location = "Login.php"
                     </script>
                <?
              }
            
             else{
                
                //if username and password exist
                if($username==admin)
                {
                    $session_username = mysql_fetch_row($result);
                    $_SESSION['username'] = $session_username[0];
                       header("Location: Admin.php");
                }
                
                else if($username==admin2)
                {
                    $session_username = mysql_fetch_row($result);
                    $_SESSION['username'] = $session_username[0];
                       header("Location: Admin.php");
                }
            
                else
                {
                     $session_username = mysql_fetch_row($result);
                    $_SESSION['username'] = $session_username[0];
                       header("Location: Customer.php");
                }
            }
        }
   }
?>

User is offlineProfile CardPM
+Quote Post

justadev
RE: Checking If User Exists Or Not
1 Jul, 2008 - 06:55 AM
Post #12

New D.I.C Head
*

Joined: 1 Jul, 2008
Posts: 1



Thanked: 1 times
My Contributions
QUOTE(hotsnoj @ 12 Sep, 2007 - 07:40 AM) *

You know, for a user who's been here a year and has over 100 posts, you'd think you'd get it that you need to use code tags.

As for your function. What does one do when they want to check if a given variable has a given value?

I'll give you a hint, it's not a function, its logic!

wow, i just stumble across this and you are a jerk. looking at your site you think your all that too.
Interests Video games, Women, Tao Kwon Do

Video Games: I don't have any friends.
Women: I can't get laid.
Tao Kwon Do: I get beat up alot.

Your big and bad when you can badmouth new developers.

Come back with something smart now A$$H0L3!
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 01:03AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month