0

Sir I am have these codes

<?php
session_start();
require_once("connect.php");

function clean($str)
{
$cstr=trim($str);
$cstr=addslashes($str);
$cstr=htmlspecialchars($str);
return $cstr;
}

if(isset($_POST['login']))
{   
$myusername=clean($_POST['username']); 
$mypassword=clean($_POST['password']); 

    if(empty($myusername) || !empty($mypassword))

    {
        echo ('<script>alert("User name or password must not be empty")</script>');
            header  ("location: login.php");
            exit;     
        }
        else
        {
        $query="SELECT * FROM admin where username ='". $myusername ."' and password='". $mypassword . " ' ";
        $result=mysqli_query($con,$query)or die ("Error". mysqli_error($con)) ;  
        $count=mysqli_num_rows($result); 
        if($count==1)
        {

                $_SESSION['username'] = $myusername;
                $_SESSION["startTime"] = date("r"); 

            header("location: contacts.php");
            exit;
        }else {
            echo '<script>alert("Your Login Name or Password is invalid")</script>';
    }
}
}else{
$result="";
$myusername=""; 
$mypassword=""; 
}
?>

and Form look likes this

<form name="form1" action="" method="post">

<table border="0" cellpadding="1" cellspacing="2" width="380px">
<tr><td width="40"></td><td width="40"></td><td width="40"></td></tr>

<tr>
<td width="40"><label>User</label></td>
<td width="40"> <input type="text" name="username" value=""  placeholder="Enter user name"></td>

<td width="30" rowspan="3" align="right"><img src="images/photo96.png" border="0"></td>    
</tr>
<tr>
<td width="40"><label>Password</label></td>
<td width="40"><input type="password" name="password" value=""  placeholder="Enter password" maxlength="11" /></td>

</tr>

<tr>
<td colspan="2">
      <input type="checkbox" value="" />
      <span style="color:#696; font-size:14px;"  >Remember Me </span><br />
</td>
</tr>

<tr>
<td colspan="3" align="center">
<input type="submit" value="Login" name="login">
<input type="reset" value="Reset">
    </td>
    </tr>

    </table>
    </form>

When I enter some user name or password, whether those are wrong or correct, then form works fine no error.

But

When I submit form without entering any value in both textboxes then why this part of codes does not work

if(empty($myusername) || !empty($mypassword))

    {
        echo ('<script>alert("User name or password must not be empty")

What is wrong my codes?
If user name or password is empty then it must say: User name or password must not be empty

Please help

2
Contributors
1
Reply
4
Views
3 Years
Discussion Span
Last Post by pritaeas
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.