Login Form (USERid, Password) getting error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • priyakollu
    New Member
    • Nov 2006
    • 9

    Login Form (USERid, Password) getting error

    hi guyz!
    please help me in sorting out error as im unable to find it...
    my requirement is there will be userid field and password field and "login" button after clicking button certain validations must be done whether he is an valid user or not (connect to database and check for valid user & password) if he is valid user he must login and goto another php file.....

    im using winxp OS / XAMPP Windows Version 1.6.4 !

    Problem: im getting errors im sending the errors & code plz sort out

    Code:
    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\sunkalpid\login.php:8) in C:\xampp\htdocs\sunkalpid\login.php on line 9
    
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\sunkalpid\login.php:8) in C:\xampp\htdocs\sunkalpid\login.php on line 9
    
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\sunkalpid\login.php on line 24

    [code=php]
    <html>
    <head>
    <title>Untitl ed Document</title>
    </head>

    <body>

    <?php
    session_start() ;

    //Connect to database

    mysql_connect ( "localhost" , "root", "sunkalpid" )or die("Could not connect: ".mysql_error() );
    mysql_select_db ("emp") or die(mysql_error ());


    $username = $_POST['username'];
    $password = md5($_POST['password']);

    $query = "SELECT * FROM users WHERE username=’$user name’ and password=’$pass word’ ";

    $result = mysql_query($qu ery);

    if (mysql_num_rows ($result) != 1)
    {
    $error = "Bad Login";
    include "login.html ";

    } else {
    $_SESSION['username'] = "$username" ;
    include "registerdispla y.html";
    }

    ?>


    </body>
    </html>
    [/code]

    looking forward ur help ASAP
    Regards
    Priya.
    Last edited by Atli; Nov 21 '07, 11:52 PM. Reason: Added [code] tags.
  • robin1983
    New Member
    • Oct 2007
    • 99

    #2
    Hi Priya,
    I think the problem is in the first line of php. means u start the session() before. do one think, just cut that protion of session start() from the begining and paste to else part just before u assign the $SESSION['username'] = $username; ok i think it will work.
    [PHP]<?php
    //session_start() ; remove this line from here

    //Connect to database

    mysql_connect ( "localhost" , "root", "sunkalpid" )or die("Could not connect: ".mysql_error() );
    mysql_select_db ("emp") or die(mysql_error ());


    $username = $_POST['username'];
    $password = md5($_POST['password']);

    $query = "SELECT * FROM users WHERE username=’$user name’ and password=’$pass word’ ";

    $result = mysql_query($qu ery);

    if (mysql_num_rows ($result) != 1)
    {
    $error = "Bad Login";
    include "login.html ";

    } else {
    session_start() ;
    $_SESSION['username'] = "$username" ;
    include "registerdispla y.html";
    }

    ?>[/PHP]


    ok try it and reply ..
    Last edited by Atli; Nov 21 '07, 11:59 PM. Reason: Removed the quote.

    Comment

    • priyakollu
      New Member
      • Nov 2006
      • 9

      #3
      After placing tht session_start() ;
      in else part i got this error

      Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs \sunkalpid\logi n.php on line 15

      Comment

      • robin1983
        New Member
        • Oct 2007
        • 99

        #4
        This means that your sql query means line no 13
        [PHP]$query = "SELECT * FROM users WHERE username=’$user name’ and password=’$pass word’ "; [/PHP]
        be sure that the username and password match the value in database. The error is because, the query dont get the result, thats why its giving this error. ok to check the error do one thing,
        [PHP]
        /// more code
        $query = "SELECT * FROM users WHERE username=’$user name’ and password=’$pass word’ ";
        $result = mysql_query($qu ery);
        $row = mysql_fetch_arr ay($result) or die(mysql_error ());
        echo $row['username']; // its just cheking the query giving the result or not ok ;
        .. more code to write..
        [/PHP]

        when the query get the result, the error will automtically rectified. ok
        Originally posted by priyakollu
        After placing tht session_start() ;
        in else part i got this error

        Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs \sunkalpid\logi n.php on line 15

        Comment

        • Ranjan kumar Barik
          New Member
          • Aug 2007
          • 95

          #5
          Hi priyakollu

          The session_start() must be the the 1st statement of the page.

          Good day
          Last edited by Atli; Nov 21 '07, 11:59 PM. Reason: Removed the quote.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Adding to the above post - move the session start to the very begging of that html file i.e. before the <html> before the <!DOCTYPE, before everything!

            :)

            Comment

            • Ranjan kumar Barik
              New Member
              • Aug 2007
              • 95

              #7
              Hi
              I have modified your code a little bit.
              Please check it out.
              [CODE=php]
              <?
              session start();
              ?>
              <html>
              <head>
              <title>Untitl ed Document</title>
              </head>
              <body>
              <?
              extract($_POST) ;
              $username = (isset($_POST['username']))?$_POST['username']:'';
              $password = (isset($_POST['password']))?$_POST['password']:'';

              //Connect to database
              $conn = mysql_connect ( 'localhost', 'root', 'sunkalpid')or die("Could not connect: ".mysql_error() );
              mysql_select_db ('emp', $conn) or die(mysql_error ());

              $query = "SELECT * FROM users WHERE username=’$user name’ and password=’$pass word’ ";

              $res = mysql_fetch_ass oc(mysql_query( $query, $conn));

              if (!$res)
              {
              $error = "Bad Login";
              include "login.html ";

              } else {
              $_SESSION['username'] = "$username" ;
              include "registerdispla y.html";
              }

              ?>


              </body>
              </html>[/code]
              Last edited by Atli; Nov 21 '07, 11:59 PM. Reason: Changed [code] tags to [code=php] and removed the quote.

              Comment

              • priyakollu
                New Member
                • Nov 2006
                • 9

                #8
                after changing the code i still got an error


                Warning: mysql_fetch_ass oc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs \sunkalpid\logi n.php on line 20
                anyone plz help me

                Comment

                • robin1983
                  New Member
                  • Oct 2007
                  • 99

                  #9
                  Hi priya, just do one think, show ur html file too. ok the problem is at line no 18 ok.
                  Originally posted by priyakollu
                  after changing the code i still got an error


                  Warning: mysql_fetch_ass oc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs \sunkalpid\logi n.php on line 20
                  anyone plz help me

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    Hi Priya.

                    Do not double post your questions. I have merged your two identical threads into this one thread.
                    Please read the Posting Guidelines before posting!

                    Also, please use &#91;code] tags when posting your code examples. (See How to ask a question)

                    &#91;code=ph p] ...PHP code goes here... &#91;/code]

                    Thank you.

                    P.S. I edited out quotes from a couple of replies, where the poster quoted the entire first post. I hope you don't mind. It was making the thread unnecessarily long.

                    Comment

                    • Ranjan kumar Barik
                      New Member
                      • Aug 2007
                      • 95

                      #11
                      Originally posted by priyakollu
                      after changing the code i still got an error


                      Warning: mysql_fetch_ass oc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs \sunkalpid\logi n.php on line 20
                      anyone plz help me
                      Hi Priya
                      Please check, If you have mentioned singles quotes ( ' ) arround $username and $password in line 18, i.e. where you are declaring the $query.

                      Yes, If The fields are numeric, then no need to do that.

                      Have a gooooooood day.
                      Last edited by Ranjan kumar Barik; Nov 22 '07, 05:35 AM. Reason: Missed something

                      Comment

                      Working...