how can i verify my login name if exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phani000
    New Member
    • Feb 2010
    • 4

    how can i verify my login name if exists

    hi
    how can i check my login name in database from php page if it already exists
    . so that i should not allow the same login name for another user . can any on help me with simple code
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    If SQL command of similar structure as this one

    "SELECT loogin_name FROM users_table WHERE loogin_name = '$entered_login _name' "

    returns anything than the chosen name already exists. Table, table field and variable names should be those in your system. I just made them up for explaining purposes.

    Hope this helps

    Comment

    • shabinesh
      New Member
      • Jan 2007
      • 61

      #3
      Code:
      $result = mysql_query("select name from table where name = new_login_name");
      $row = mysql_fetch_array($result);
      if($row[0]) 
            echo "Err: select different name";
      else 
           echo "okay, no prob with login name";
      I guess this could be a hint :)

      Comment

      • phani000
        New Member
        • Feb 2010
        • 4

        #4
        thanx for your help but i am getting this error


        Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource in


        line 8
        okay, no prob with login nameMail Sent.Error: Column count doesn't match value count at row 1

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          That was not a copy/paste-able code. You need to re-write the SQL query and add error checking before it can work.

          Post your code here and we may be able to point you in the right direction.

          Comment

          • phani000
            New Member
            • Feb 2010
            • 4

            #6
            Code:
            <?php
             include 'conifig.php';
             
             $pass=md5($_POST[password]);
            $sql="INSERT INTO meconzee1_user VALUES ('','$_POST[firstname]','$_POST[lastname]','$_POST[middlename]','$_POST[radio]' ,'$_POST[loginname]','$pass',
            '$_POST[dateofbirth]','$_POST[address1]','$_POST[address2]','$_POST[state]','$_POST[city]','$_POST[country]','$_POST[pincode]','$_POST[mailid]','$_POST[answer1]','$_POST[answer2]','$_POST[answer3]')";
             $result = mysql_query("select name from table where name = new_$_POST[loginname]");
             $row = mysql_fetch_array($result);
             if($row[0]) 
                   echo "Err: select different name";
             else 
                  echo "okay, no prob with login name";
            
            $to = $_POST[mailid];
            $subject = "Test mail";
            $message = " Hello! This is a simple email message.
                           your account has been created with the following login credentials 
            			   username ='$_POST[loginname]'
            			   password ='$pass'";
            $from = "phanisundar_sanka@example.com";
            $headers = "From: $from";
            mail($to,$subject,$message,$headers);
            echo "Mail Sent.";
            if (!mysql_query($sql,$con))
              {
              die('Error: ' . mysql_error());
              }
            
            echo"1 record is added";
            
            mysql_close($con);
              ?>
            this is my code
            Last edited by Atli; Feb 5 '10, 01:53 PM. Reason: Added [code] tags and replaced real email address with an example.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Ok. There are a lot of problems in that code.

              For example:
              • The code shabinesh posted was NOT supposed to be just copied into the middle of your project, unaltered. It was an example, which you have to adapt to your own project. The way you use it will not work. (Not well, at least.)
              • All text values in SQL queries need to be quoted. If they are not, the query will fail. (See your SELECT query)
              • You should NEVER put raw user input into a SQL query. If you do, you open yourself up to an SQL Injection attack. - All data meant to be included in a MySQL query should be run through the mysql_real_esca pe_string function first. - This is PHP security 101; the #1 security threat a PHP application faces. It is the first thing a hacker will try to exploit when attacking your web, so you should ALWAYS keep this in mind.
              • Strings should always be quoted. This includes array element names:
                [code=php]
                $stuff = $_POST[stuff]; // INCORRECT
                $stuff = $_POST['stuff']; // CORRECT

                $string = "Stuff: $_POST[stuff]"; // This may work...
                $string = "Stuff: {$_POST['stuff']}"; // But this is better.
                [/code]

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Furthermore, you should be checking that your $result variable is valid, i.e., your query ran successfully.

                Code:
                $result = mysql_query( ... );
                if ($result === FALSE) {
                    /** Query failed */
                    print mysql_error();
                    exit;
                }
                $row = mysql_fetch_array();
                /** ... */
                Sounds like you need to brush up on your PHP/MySQL skills.

                Comment

                • shabinesh
                  New Member
                  • Jan 2007
                  • 61

                  #9
                  yes! its mere a hint, not copy-paste code. :D

                  Comment

                  Working...