SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2001
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Off One Error?!?!

    Is there anything wrong with this code?

    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
    <p>Username <INPUT TYPE=TEXT NAME="name" SIZE="20"><BR>
    Password <INPUT TYPE=TEXT NAME="pass" SIZE="20"></p>
    <INPUT TYPE=SUBMIT NAME="submit" VALUE="Submit">
    </FORM>

    <?php
    if (isset($name)) {
    $dbcnx = @mysql_connect("mysql", "myusername", "mypassword");
    if (!$dbcnx) { echo("Access fail"); exit(); }
    if (! @mysql_select_db("nameofthedb") ) { echo( "Unable to locate database"); exit(); }
    $getit = mysql_query( "SELECT Username, Password FROM Login");
    if (!$getit) {echo("Error performing query" . mysql_error() ); exit();}
    while ($rows = mysql_fetch_array($getit)) { $username = $rows["Username"]; $password = $rows["Password"]; }
    if ($name == $username AND $pass == $password) { echo("Welcome" . $username); }
    else { echo("<p>Incorrect username or password</p>");}
    } ?>

    I enter the correct name and the password of my first entry in my database, it prints out "Incorrect username or password"
    I try others, and they work fine

    So I experimented a litte bit, I change the oringnal one to
    if ($name != $username AND $pass != $password) { echo("Welcome" . $username); }
    Now instead of the first entry, the the name and the password of my last entry in my database doesn't work.
    I guess it is an off by one error in the while loop.

    Please help me, im a newbie...
    Last edited by SuperNewbie; Sep 21, 2001 at 22:52.
    signature

  2. #2
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's going to print out that inccorect username no matter what because of where you have your brackets.
    this
    PHP Code:
    else { echo("<p>Incorrect username or password</p>");} 
    ?> 
    should be
    PHP Code:
    } else { echo("<p>Incorrect username or password</p>");} 
    ?> 
    Also, the way you have it, it's going to run the all the php code before you submit a value. here's your code in the correct form:
    PHP Code:
    <?php
    if (!$submit)
    {
    ?>

    <FORM ACTION="<?php echo $PHP_SELF?>" METHOD=POST> 
    <p>Username <INPUT TYPE=TEXT NAME="name" SIZE="20"><BR> 
    Password <INPUT TYPE=TEXT NAME="pass" SIZE="20"></p> 
    <INPUT TYPE=SUBMIT NAME="submit" VALUE="Submit"> 
    </FORM> 

    <?
    } else {

        if (isset(
    $name)) 
        {
         
    $dbcnx = @mysql_connect("mysql""myusername""mypassword"); 
            if (!
    $dbcnx
            { 
             echo(
    "Access fail"); exit(); 
            } 
                if (! @
    mysql_select_db("nameofthedb")) 
                {
                 echo( 
    "Unable to locate database");
                 exit();
                } 
         
    $getit mysql_query"SELECT Username, Password FROM Login WHERE ID=1"); 
                    if (!
    $getit)
                    {
                     echo(
    "Error performing query" mysql_error() );
                     exit();
                    } 
            while (
    $rows mysql_fetch_array($getit))
            {
             
    $username $rows["Username"]; $password $rows["Password"];
            } 
            
            if (
    $name == $username AND $pass == $password)
            {
             echo(
    "Welcome" $username);
            } 

        } else {
               echo(
    "<p>Incorrect username or password</p>");
               } 
    }
    ?>
    Try that and lemme know.
    Last edited by Defender1; Sep 21, 2001 at 22:38.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  3. #3
    SitePoint Member
    Join Date
    Aug 2001
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It still doesnt work...
    It works only if i enter the name of the last row of my table.
    signature

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about a link to the offending script and/or what it's doing and/or not doing?

    you just asked what was wrong in the code. i could only find syntax errors. that won't solve problems with querying your db etc.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    SitePoint Member
    Join Date
    Aug 2001
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found the problem, i have put this
    if ($name == $username AND $pass == $password)
    {
    echo("Welcome" . $username);
    }

    } else {
    echo("<p>Incorrect username or password</p>");
    }

    inside the while loop.
    I try to put it inside the while loop, but i get an error. plz help
    signature

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have got no where clause in your query. Using your form I might do something like this to check a username and password:

    PHP Code:
    <?php

        $dbcnx 
    mysql_connect('host','username','password');
        
    mysql_select_db('db');

        
    $sql mysql_query("SELECT * FROM Login WHERE Username = '$name' AND Password ='$pass'") or die(mysql_error());

        
    $count mysql_num_rows($sql);

        
    $result mysql_fetch_array($sql);

        if ( 
    $count == ) {

            echo 
    "Incorrect username/password";

        } else {

            
    extract($result);
            echo 
    "Hello $Username";

        }

    ?>
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •