?
Solved

Code not working

Posted on 2006-07-21
13
Medium Priority
?
237 Views
Last Modified: 2006-11-18
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
Comment
Question by:avo42
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +2
13 Comments
 

Author Comment

by:avo42
ID: 17154857
Forgot to mention this part works fine

          elseif ($_POST['txt_username']=="")
          {    
               $username_error="true";
          }
          elseif ($_POST['txt_password']=="")
          {    
               $password_error="true";
          }
0
 
LVL 9

Expert Comment

by:Rob_Jeffrey
ID: 17155042
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
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 17155095
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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 2

Assisted Solution

by:tansofun
tansofun earned 600 total points
ID: 17155145
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
 

Author Comment

by:avo42
ID: 17155636
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
 
LVL 9

Expert Comment

by:Rob_Jeffrey
ID: 17155659
Could you post the entire code (if not too large) minus any back end login info.
0
 

Author Comment

by:avo42
ID: 17155724
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
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 17155880
($_POST['txt_username']=="" || $_POST['txt_password']=="")


It will return error if username OR password is empty.

Goodluck
Bonmat86.
0
 

Author Comment

by:avo42
ID: 17155938
Hi THanks

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

duno why
0
 
LVL 10

Assisted Solution

by:Khanh Doan
Khanh Doan earned 600 total points
ID: 17155995
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
 
LVL 9

Accepted Solution

by:
Rob_Jeffrey earned 800 total points
ID: 17155996
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
 
LVL 9

Expert Comment

by:Rob_Jeffrey
ID: 17156119
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
 

Author Comment

by:avo42
ID: 17157295
Hi thanks all for your help

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

appriciated
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question