• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Code not working

Hi All

Any ideas on this one please this part of the code is never evaluating

($_POST['txt_username']=="" && $_POST['txt_password']=="")

all it is checking is if text boxes contain nothing
but dont understand why this part is never executed when both are not containing anything and the txt boxe names are correct


FULL CODE
if ($_POST['btn_login'])
{
      mysql_connect($dbhost, $dbuser, $dbpass);
      mysql_select_db($dbname);
      $sql = "SELECT * FROM downloads WHERE username = '".$_POST['txt_username']."' AND password = '".md5($_POST['txt_password'])."'";
      $result = mysql_query($sql)or die (mysql_error());
      $row=mysql_fetch_array($result);
      
      if (mysql_num_rows($result)>0)
      {
                  $_SESSION['user']="true";
                  $_SESSION['username']=$row['realname'];
                  header('location: main.php');
      }
            elseif ($_POST['txt_username']=="" && $_POST['txt_password']=="")
            {      
                  $username_password_error="true";
            }
            elseif ($_POST['txt_username']=="")
            {      
                  $username_error="true";
            }
            elseif ($_POST['txt_password']=="")
            {      
                  $password_error="true";
            }
            else
            {
                  $username_password_notfound="true";
            }

}
Thanks in advance
0
avo42
Asked:
avo42
  • 5
  • 4
  • 2
  • +2
3 Solutions
 
avo42Author Commented:
Forgot to mention this part works fine

          elseif ($_POST['txt_username']=="")
          {    
               $username_error="true";
          }
          elseif ($_POST['txt_password']=="")
          {    
               $password_error="true";
          }
0
 
Rob_JeffreyIT/ProgrammingCommented:
I can't see why it doesn't work - looks like it should.
When you are testing the output, $username_password_error is spelled correctly?
(Probably a dumb question but I keep finding these in my code)
0
 
Cornelia YoderArtistCommented:
Since you are using && (AND), you would need BOTH conditions to be true to succeed, whereas in the part that you say works, only one of them would need to be true.  Have you checked with both conditions true at the same time?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tansofunCommented:
I am unsure as well, but try

elseif (($_POST['txt_username']=="") && ($_POST['txt_password']==""))
or

elseif ((strlen($_POST['txt_username'])==0) && (strlen($_POST['txt_password'])==0))

Also, wouldn't you want to move these checks before the DB call?
0
 
avo42Author Commented:
Hi Thanks


no still the same with the above codes replaced

but another straing thing if i do the opposite !="" then it will work then if i  do add something in both the txt boxes the code performs as its written not as i would like but as expected but sill will not work with ==""

please have a look and test i you would like to

http://downloads.teamconsole.eu/login.php

All help appriciated
0
 
Rob_JeffreyIT/ProgrammingCommented:
Could you post the entire code (if not too large) minus any back end login info.
0
 
avo42Author Commented:
Thanks All code - db info

if (isset($_POST['btn_login']))
{
      mysql_connect($dbhost, $dbuser, $dbpass);
      mysql_select_db($dbname);
      $sql = "SELECT * FROM downloads WHERE username = '".$_POST['txt_username']."' AND password = '".md5($_POST['txt_password'])."'";
      $result = mysql_query($sql)or die (mysql_error());
      $row=mysql_fetch_array($result);
      
      if (mysql_num_rows($result)>0)
      {
                  $_SESSION['user']="true";
                  $_SESSION['username']=$row['realname'];
                  header('location: main.php');
      }
            elseif ($_POST['txt_username']=="" && $_POST['txt_password']=="")
            {      
                  $username_password_error="true";
            }
            elseif ($_POST['txt_username']=="")
            {      
                  $username_error="true";
            }
            elseif ($_POST['txt_password']=="")
            {      
                  $password_error="true";
            }
            else
            {
                  $username_password_notfound="true";
            }

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Downloads Login</title>
<style type="text/css">
<!--
@import url("style.css");

-->
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<?
if (isset($_POST['btn_login']) || !isset($_POST['btn_forgot_password_x']) || !isset($_POST['btn_forgot_password_y']))
{
?>
<table border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <th scope="col"><img src="images/top_left.gif" width="10" height="25" /></th>
    <th width='275' align='center' valign='middle' background='/images/top.gif' class='white_12_Tahoma'>Please Enter Login Details </th>
    <th scope="col"><img src="images/top_right.gif" width="15" height="25" /></th>
  </tr>
  <tr>
    <td><img src="images/left.gif" width="10" height="170" /></td>
    <td><table border="0" align="center" cellpadding="2" cellspacing="2">
      <tr>
        <th colspan="2" align="left" valign="top" scope="col">&nbsp;</th>
      </tr>
      <tr>
        <th align="left" valign="top">Username:</th>
        <td align="left" valign="top"><input name="txt_username" type="text" id="txt_username" value="<?=$_POST['txt_username']?>"/></td>
      </tr>
      <tr>
        <th align="left" valign="top">Password:</th>
        <td align="left" valign="top"><input name="txt_password" type="password" id="txt_password"/></td>
      </tr>
      <tr>
        <td align="center" valign="middle">&nbsp;</td>
        <td align="center" valign="middle"><span class="style1">
          <input name="btn_login" type="submit" class="style7" id="btn_login" value="Submit" />
        </span></td>
      </tr>
      <tr>
        <td colspan="2" align="left" valign="top">
<input type="image" src="images/magnify_glass.jpg" name="btn_forgot_password" id="btn_forgot_password" value="Submit" />
<span class="style7">Forgotten Password</span></td>
      </tr>
     
    </table></td>
    <td><img src="images/right.gif" width="15" height="170" /></td>
  </tr>
  <tr>
    <td><img src="images/bottom_left.gif" width="10" height="15" /></td>
    <td><img src="images/bottom.gif" width="275" height="15" /></td>
    <td><img src="images/bottom_right.gif" width="15" height="15" /></td>
  </tr>
</table>
<? } ?>
<br/>
<?
//if (isset($_POST['btn_forgot_password']))
if (isset($_POST['btn_forgot_password_x']) && isset($_POST['btn_forgot_password_y']))
{?>
<table border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <th scope="col"><img src="images/top_left.gif" width="10" height="25" /></th>
        <th width='350' align='center' valign='middle' background='/images/top.gif' class='white_12_Tahoma'>Please Enter E-Mail Details </th>
    <th scope="col"><img src="images/top_right.gif" width="15" height="25" /></th>
  </tr>
  <tr>
    <td><img src="images/left.gif" width="10" height="170" /></td>
    <td><table border="0" align="center" cellpadding="2" cellspacing="2">
      <tr>
        <th colspan="2" align="left" valign="top" scope="col">&nbsp;</th>
      </tr>
      <tr>
        <th align="left" valign="top">E-Mail Address:</th>
        <td align="left" valign="top"><input name="txt_email" type="text" id="txt_email" /></td>
      </tr>
      <tr>
        <th align="left" valign="top">Confirm E-Mail Address:</th>
        <td align="left" valign="top"><input name="txt_confirm_email" type="text" id="txt_confirm_email" /></td>
      </tr>
      <tr>
        <td align="center" valign="middle"><input name="btn_cancel" type="image" src="images/magnify_glass.jpg" class="style6" id="btn_cancel" value="Reset" border="border" />
            <span class="style7">Cancel </span></td>
        <td align="center" valign="top" class="style1"><input name="btn_forgot" type="submit" class="style7" id="btn_forgot" value="Submit" /></td>
      </tr>
    </table></td>
    <td><img src="images/right.gif" width="15" height="170" /></td>
  </tr>
  <tr>
    <td><img src="images/bottom_left.gif" width="10" height="15" /></td>
    <td><img src="images/bottom.gif" width="350" height="15" /></td>
    <td><img src="images/bottom_right.gif" width="15" height="15" /></td>
  </tr>
</table>
<? } ?>
<br />
<?
if ($email_request=="true")
{
?>
        <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <th scope="col"><img src="images/top_left.gif" width="10" height="25" /></th>
        <th width='400' align='center' valign='middle' background='/images/top.gif' class='white_12_Tahoma'>Please Check Your E-Mail</th>
          <th scope="col"><img src="images/top_right.gif" width="15" height="25" /></th>
        </tr>
        <tr>
          <td><img src="images/left.gif" width="10" height="50" /></td>
          <td><table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <th scope="col"><span class="style5">Thankyou your username and password will be e-mail to your given address.</span></th>
            </tr>
           
          </table></td>
          <td><img src="images/right.gif" width="15" height="50" /></td>
        </tr>
        <tr>
          <td><img src="images/bottom_left.gif" width="10" height="15" /></td>
          <td><img src="images/bottom.gif" width="400" height="15" /></td>
          <td><img src="images/bottom_right.gif" width="15" height="15" /></td>
        </tr>
      </table>
        <? } ?>
      <br />
 <?
if ($no_email_found==="true")
{
?>
        <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <th scope="col"><img src="images/top_left.gif" width="10" height="25" /></th>
        <th width='400' align='center' valign='middle' background='/images/top.gif' class='white_12_Tahoma'>ERROR!!</th>
          <th scope="col"><img src="images/top_right.gif" width="15" height="25" /></th>
        </tr>
        <tr>
          <td><img src="images/left.gif" width="10" height="30" /></td>
          <td><table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <th scope="col"><span class="style3">No matching e-mail address Found.</span></th>
            </tr>
           
          </table></td>
          <td><img src="images/right.gif" width="15" height="30" /></td>
        </tr>
        <tr>
          <td><img src="images/bottom_left.gif" width="10" height="15" /></td>
          <td><img src="images/bottom.gif" width="400" height="15" /></td>
          <td><img src="images/bottom_right.gif" width="15" height="15" /></td>
        </tr>
      </table>
        <? } ?>
      <br />
<?
if ($username_password_notfound==="true")
{
?>
        <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <th scope="col"><img src="images/top_left.gif" width="10" height="25" /></th>
<th width='400' align='center' valign='middle' background='/images/top.gif' class='white_12_Tahoma'>ERROR!!</th>
          <th scope="col"><img src="images/top_right.gif" width="15" height="25" /></th>
        </tr>
        <tr>
          <td><img src="images/left.gif" width="10" height="30" /></td>
          <td><table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <th scope="col"><span class="style3">No Username or Password Found Please Check. </span></th>
            </tr>
           
          </table></td>
          <td><img src="images/right.gif" width="15" height="30" /></td>
        </tr>
        <tr>
          <td><img src="images/bottom_left.gif" width="10" height="15" /></td>
          <td><img src="images/bottom.gif" width="400" height="15" /></td>
          <td><img src="images/bottom_right.gif" width="15" height="15" /></td>
        </tr>
      </table>
        <?
}

if ($username_error==="true")
{
?>
      <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <th scope="col"><img src="images/top_left.gif" width="10" height="25" /></th>
          <th width='400' align='center' valign='middle' background='/images/top.gif' class='white_12_Tahoma'>ERROR!!</th>
          <th scope="col"><img src="images/top_right.gif" width="15" height="25" /></th>
        </tr>
        <tr>
          <td><img src="images/left.gif" width="10" height="30" /></td>
          <td><table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <th scope="col"><span class="style3">Please Check Username Field. </span></th>
              </tr>
          </table></td>
          <td><img src="images/right.gif" width="15" height="30" /></td>
        </tr>
        <tr>
          <td><img src="images/bottom_left.gif" width="10" height="15" /></td>
          <td><img src="images/bottom.gif" width="400" height="15" /></td>
          <td><img src="images/bottom_right.gif" width="15" height="15" /></td>
        </tr>
      </table>
<? }
if ($password_error==="true")
{
?>
      <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <th scope="col"><img src="images/top_left.gif" width="10" height="25" /></th>
          <th width='400' align='center' valign='middle' background='/images/top.gif' class='white_12_Tahoma'>ERROR!!</th>
          <th scope="col"><img src="images/top_right.gif" width="15" height="25" /></th>
        </tr>
        <tr>
          <td><img src="images/left.gif" width="10" height="30" /></td>
          <td><table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <th scope="col"><span class="style3">Please Check Password Field. </span></th>
              </tr>
          </table></td>
          <td><img src="images/right.gif" width="15" height="30" /></td>
        </tr>
        <tr>
          <td><img src="images/bottom_left.gif" width="10" height="15" /></td>
          <td><img src="images/bottom.gif" width="400" height="15" /></td>
          <td><img src="images/bottom_right.gif" width="15" height="15" /></td>
        </tr>
      </table>
<? }  
if ($username_password_error==="true")
{
?>
      <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <th scope="col"><img src="images/top_left.gif" width="10" height="25" /></th>
          <th width='400' align='center' valign='middle' background='/images/top.gif' class='white_12_Tahoma'>ERROR!!</th>
          <th scope="col"><img src="images/top_right.gif" width="15" height="25" /></th>
        </tr>
        <tr>
          <td><img src="images/left.gif" width="10" height="30" /></td>
          <td><table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <th scope="col"><span class="style3">Please Check Username And Password Fields. </span></th>
              </tr>
          </table></td>
          <td><img src="images/right.gif" width="15" height="30" /></td>
        </tr>
        <tr>
          <td><img src="images/bottom_left.gif" width="10" height="15" /></td>
          <td><img src="images/bottom.gif" width="400" height="15" /></td>
          <td><img src="images/bottom_right.gif" width="15" height="15" /></td>
        </tr>
      </table>
<? } ?>
</form>
</body>
</html>
0
 
Khanh DoanSenior DeveloperCommented:
($_POST['txt_username']=="" || $_POST['txt_password']=="")


It will return error if username OR password is empty.

Goodluck
Bonmat86.
0
 
avo42Author Commented:
Hi THanks

it should i agree but for some reason it will not return the error

duno why
0
 
Khanh DoanSenior DeveloperCommented:
How come it won't return error ?
So in the form

<script language="javascript">
function check()
{
    username = document.getElementById('username').value;
    password = document.getElementById("password').value;
    if (username == '')
    {
        alert ("Please enter username");
        return false
    }
    if (password == '')
    {
        alert ("Please enter password");
        return false
    }
}
</script>
<form name="submit" .... onSubmit="return check()">
user : <input type="text" id="username">
pass : <input type="password" id="password">
<input type="submit">
</form>

Bonmat86.
0
 
Rob_JeffreyIT/ProgrammingCommented:
Try this (using tansofun's suggestion)

<?php if (isset($_POST['btn_login']))
{
      if ($_POST['txt_username']=="" && $_POST['txt_password']=="") {    
            $username_password_error="true";
      } elseif ($_POST['txt_username']=="") {    
            $username_error="true";
      } elseif ($_POST['txt_password']=="") {    
            $password_error="true";
      } else {
          mysql_connect($dbhost, $dbuser, $dbpass);
            mysql_select_db($dbname);
            $sql = "SELECT * FROM downloads WHERE username = '".$_POST['txt_username']."' AND password = '".md5($_POST['txt_password'])."'";
            $result = mysql_query($sql)or die (mysql_error());
            $row=mysql_fetch_array($result);
            
            if (mysql_num_rows($result)>0){
                  $_SESSION['user']="true";
                  $_SESSION['username']=$row['realname'];
                  header('location: main.php');
            } else {
                  $username_password_notfound="true";
        }
      }
}
?>
0
 
Rob_JeffreyIT/ProgrammingCommented:
This line should be changed:

<input name="txt_username" type="text" id="txt_username" value="<?=$_POST['txt_username']?>"/>

to

<input name="txt_username" type="text" id="txt_username" <?php if ($_POST['txt_username']<>"") { echo 'value="' . $_POST['txt_username'] . '"'; ?> />

The first time through you are placing a null value in the field setting the Post data.
0
 
avo42Author Commented:
Hi thanks all for your help

it must have been because the db was called before the checks were made

appriciated
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now