为什么我的代码显示已经发送的标题,即使它处于if else条件下?

时间:2014-04-04 09:41:49

标签: php mysql

我有一个非常简单的代码,用于检查表中是否存在电子邮件地址。如果它存在,则警告已经存在并重定向到另一个页面,如果它不存在,则插入表格。当表中没有电子邮件地址时插入正确发生并且被重定向到受尊重的页面但是当表中存在电子邮件时,虽然它警告用户名存在,但它显示已经错误发送的标题

我的代码是

<?php   
    require_once('connection.php'); 
    $tbl_name="cust_contacts";      
        $ccode=$_POST['ccode'];
        $name=$_POST['name'];
        $des=$_POST['desig'];
        $dep=$_POST['dep'];
        $phone=$_POST['phone'];
        $mobile=$_POST['mob'];
        $email=$_POST['email']; 
        $check="select * from $tbl_name where email='$email'";
        $checkqry=mysql_query($check);
        if($checkqry['error'])
            die(mysql_error()); 
        $countcheck=mysql_num_rows($checkqry);
        if($countcheck==0)
        {
            $qry="insert into $tbl_name(cust_code,name,designation,department,phone,mobile,email) values('$ccode','$name','$des','$dep','$phone','$mobile','$email')";
            if (!mysql_query($qry))
            {
                die('Error: ' . mysql_error());
            }       
            header("location:index.php?customername=$ccode#pop4");
        }
        else
        {
            echo '<script type="text/javascript">alert("User already exists. Try editing the user");</script>';
            header("location:index.php?customername=$ccode");
        }                   
    //$count=mysql_num_row($result);
    //if($count==1) 
?>

2 个答案:

答案 0 :(得分:0)

如果要显示提醒消息并重定向到其他页面,可以使用以下JavaScript:

Befor(您的原始代码):

    echo '<script type="text/javascript">alert("User already exists. Try editing the user");</script>';
    header("location:index.php?customername=$ccode");

后:

 echo '<script type="text/javascript">';
 echo  'alert("User already exists. Try editing the user");';
 echo  'location.href = "index.php?customername=' . $ccode. '";';
 echo '</script>';

希望这有帮助。

答案 1 :(得分:0)

我遇到了同样的问题,据我所知,如果在某些代码之后使用header(),则会抛出错误,因此可能尝试删除echo语句。

如果这不起作用,我使用了函数调用。这个功能:

function redirect($url) {
  if (!headers_sent()) {
      header('Location: '.$url);
      exit;
} else {
    echo '<script type="text/javascript">';
    echo 'window.location.href="'.$url.'";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
    echo '</noscript>'; exit;
}

}

这是电话会议的一个例子:

if($mail->send()) {
        // redirect to thank you message.
        redirect( BASE_URL . "thanks/");
        exit;
    } else {
        $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
    }

如果使用PDO类成功发送电子邮件,我用它来重定向,但它可以在任何地方使用。