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

时间:2015-09-16 11:36:51

标签: javascript php mysql database

在下面的代码中,else部分工作正常,但如果数据库中没有电子邮件,为什么javascript警报---('此电子邮件ID不存在于我们的记录中)#not;工作? 。如果else部分正在工作,为什么num_rows = 0不起作用。我尝试了很多。我们将不胜感激。

<?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>

6 个答案:

答案 0 :(得分:2)

更改此

if(is_null($num))

到这个

if(empty($num))

更改此

if($email_id)

到这个

if(isset($email_id))

答案 1 :(得分:1)

我会先检查控制台消息,因为我认为你的警报功能根本不起作用,即使它出现了&#39;你在文中使用过的。

更改此

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

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

干杯

答案 2 :(得分:0)

将变量$ num与0和FALSE进行比较。因为mysql_num_rows函数在成功时返回整数,并在失败时返回FALSE。

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

AND再次检查$ email empty或isset

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

答案 3 :(得分:0)

$num = mysql_num_rows($qry);为您提供其他FALSE检索到的行数的计数,表示0条记录。

因此,当您执行检查if(is_null($num))时,它将永远不会输入if子句,因为mysql_num_rows($qry);永远不会返回NULL

您需要将其更改为

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

答案 4 :(得分:0)

首先在PHP代码中引入Javascript警告框,你需要调用一个函数。请尝试以下代码

<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
 }

答案 5 :(得分:0)

试试这个

        <?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>