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

Join 119,783 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,381 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! 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
post 12 Sep, 2007 - 07: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

Go to the top of the page


snoj
post 12 Sep, 2007 - 07:40 AM
Post #2


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 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

Go to the top of the page

brainy_creature
post 12 Sep, 2007 - 08: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

Go to the top of the page

snoj
post 12 Sep, 2007 - 08:40 AM
Post #4


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 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

Go to the top of the page

ap0c0lyps3
post 12 Sep, 2007 - 08:40 AM
Post #5


D.I.C Head

Group Icon
Joined: 19 Jun, 2007
Posts: 78



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 - 08:42 AM
User is offlineProfile CardPM

Go to the top of the page

snoj
post 12 Sep, 2007 - 10:36 AM
Post #6


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 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

Go to the top of the page

brainy_creature
post 13 Sep, 2007 - 08: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

Go to the top of the page

Amadeus
post 13 Sep, 2007 - 01:11 PM
Post #8


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 11,929



Thanked 24 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

Go to the top of the page

ap0c0lyps3
post 14 Sep, 2007 - 04:37 AM
Post #9


D.I.C Head

Group Icon
Joined: 19 Jun, 2007
Posts: 78



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 - 01:13 PM
User is offlineProfile CardPM

Go to the top of the page

brainy_creature
post 14 Sep, 2007 - 06: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

Go to the top of the page

antiturncoat
post 17 Sep, 2007 - 07: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

Go to the top of the page

justadev
post 1 Jul, 2008 - 07: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

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 10/15/08 09:44PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month