简体   繁体   English

这个php代码有什么问题,如下所示?

[英]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 ? 在下面的代码中,else部分工作正常,但如果电子邮件不在数据库中,为什么javascript警报---('此电子邮件ID不存在于我们的记录中')不起作用? . If the else part is working why the num_rows=0 not working . 如果else部分正在工作,为什么num_rows = 0不起作用。 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. 将变量$ num与0和FALSE进行比较。 Since mysql_num_rows function returns in integer on success and return FALSE on failure. 由于mysql_num_rows函数在成功时返回整数,因此在失败时返回FALSE。

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

AND Check $email agains empty or isset 并检查$ email agains empty或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. 为您提供其他行检索的计数数,否则为FALSE ,表示0条记录。

So when you do a check if(is_null($num)) it will never enter the if clause, because the mysql_num_rows($qry); 所以当你检查if(is_null($num))它永远不会输入if子句,因为mysql_num_rows($qry); will never return NULL . 永远不会返回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. 首先要在PHP代码中引入Javascript警告框,你需要调用一个函数。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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