SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: Off One Error?!?!
-
Sep 21, 2001, 23:18 #1
- 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 23:52.
signature
-
Sep 21, 2001, 23:35 #2
- 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.
thisPHP Code:else { echo("<p>Incorrect username or password</p>");}
} ?>
PHP Code:} else { echo("<p>Incorrect username or password</p>");}
?>
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>");
}
}
?>Last edited by Defender1; Sep 21, 2001 at 23:38.
-
Sep 22, 2001, 00:22 #3
- 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
-
Sep 22, 2001, 00:31 #4
- 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.
-
Sep 22, 2001, 05:00 #5
- 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 helpsignature
-
Sep 22, 2001, 06:32 #6
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 == 0 ) {
echo "Incorrect username/password";
} else {
extract($result);
echo "Hello $Username";
}
?>Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
Bookmarks