简体   繁体   中英

what is wrong with this php code shown as below?

In the code below , the else part is working fine , but if the email is not there in database , why the javascript alert --- ('This email id doesnt exist in our records') not working ? . If the else part is working why the num_rows=0 not working . I tried a lot . A help will be appreciated.

<?php
include 'db.php';
$em = $_POST['email'];

$qry = mysql_query("select * from user where  email_id='$em' ")or die(mysql_error());
$num = mysql_num_rows($qry);
if(is_null($num))
{
    ?>
    <script type="text/javascript">
    alert('This email id doesn't exist  in our records');
    </script> 
    <?php
}
else
{
    while ($row = mysql_fetch_array($qry))
    {
        $email_id = $row['email_id'];
        $name = $row['user_login_id'];
        $password= $row['password'];
    }

    if($email_id)
    {
        $to= $email_id;
        $subject=" Password Recovery Mail";
        $message="Let US REMIND YOU!!  Your  username - ".$name. " and     Password - ".$password;
        $header = "MIME-Version: 1.0" . "\r\n";
        $header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $header.="From:<donotreply@jassasia.com>"."\r\n"; 
        $n=mail($to,$subject,$message,$header);
        ?> <script type="text/javascript">
        alert('Check your Mail, <?php echo $email_id; ?>, for your username and  password');
        window.location="index.php";
        </script> 
        <?php
    }
}
?>
<div class="container">
    <div class="row">
        <div class="col-md-8"><br/>
            <div class="col-md-12" style="border:1px solid #ddd;"><br/>
                <form class="form-horizontal" action="" method="POST">
                    <h2>Hope you remember your registered email id...</h2>
                    <div class="form-group">
                        <label class="col-sm-5 control-label">Enter the registered email id</label>
                        <div class="col-sm-2">
                            <input type="email" name="email" autocomplete="off"  id="" placeholder="Enter the email id " required autofocus>
                            <input type="submit" id="sub" name="forgotpass" value="Continue">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

change this

if(is_null($num))

to this

if(empty($num))

And

change this

if($email_id)

to this

if(isset($email_id))

I would fist check the console messages since I think your alert function is not working at all even though it comes on it cause of the ' that you used in text.

Change this

  alert('This email id doesn't exist  in our records');

to

  alert("This email id doesn't exist  in our records"); // See the " 

Cheers

Compare your variable $num against 0 and FALSE. Since mysql_num_rows function returns in integer on success and return FALSE on failure.

if( $num ==0 && $num!=FALSE)

AND Check $email agains empty or isset

if(isset($email) && $email!="")

$num = mysql_num_rows($qry); gives you the number of count of no of rows retrieved else FALSE , indicating 0 records.

So when you do a check if(is_null($num)) it will never enter the if clause, because the mysql_num_rows($qry); will never return NULL .

you need to change that to

<? if($num == FALSE)
    {
?>
     <script type="text/javascript">
       alert('This email id doesn't exist  in our records');
     </script> 

First of all to introduce Javascript alert box inside PHP code you need to call a function. Try the below code

<script type="text/javascript">
    function show_alertMsg() {
        var msg = "This email id doesn't exist  in our records.";
        alert(msg);
    }
</script>
 if(is_null($num))
   {
     echo '<script type="text/javascript"> show_alertMsg(); </script>';
   }
else {
   // your code
 }

try this

        <?php
     include 'db.php';
       $em = $_POST['email'];

    $qry = mysql_query("select * from user where  email_id='$em' ")or    die(mysql_error());

    if( mysql_num_rows($qry)  < 0)
    {
      ?>
        <script type="text/javascript">
         alert('This email id doesn't exist  in our records');
</script> 
<?php
}
 else
 {
while ($row = mysql_fetch_array($qry))
{
    $email_id = $row['email_id'];
    $name = $row['user_login_id'];
    $password= $row['password'];
}

if($email_id)
{
    $to= $email_id;
    $subject=" Password Recovery Mail";
    $message="Let US REMIND YOU!!  Your  username - ".$name. " and     Password - ".$password;
    $header = "MIME-Version: 1.0" . "\r\n";
    $header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $header.="From:<donotreply@jassasia.com>"."\r\n"; 
    $n=mail($to,$subject,$message,$header);
    ?> <script type="text/javascript">
    alert('Check your Mail, <?php echo $email_id; ?>, for your username and  password');
    window.location="index.php";
    </script> 
    <?php
}
}
?>
  <div class="container">
    <div class="row">
    <div class="col-md-8"><br/>
        <div class="col-md-12" style="border:1px solid #ddd;"><br/>
            <form class="form-horizontal" action="" method="POST">
                <h2>Hope you remember your registered email id...</h2>
                <div class="form-group">
                    <label class="col-sm-5 control-label">Enter the registered email id</label>
                    <div class="col-sm-2">
                        <input type="email" name="email" autocomplete="off"  id="" placeholder="Enter the email id " required autofocus>
                        <input type="submit" id="sub" name="forgotpass" value="Continue">
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM