dongpuchao1680 2014-04-04 09:41
浏览 5
已采纳

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

This question already has an answer here:

I have a very simple code which checks whether email address exists in the table or not. If it exists then alert Already Exists and redirect to another page and if it doesn't exist then insert into table. Inserting when the email address is not present in the table is happening properly and is getting redirected to the respected page but when the email is existing in the table then although it alerts that username exists, it shows headers already sent at error

My code is

<?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) 
?>
</div>
  • 写回答

2条回答 默认 最新

  • dougai2427 2014-04-04 09:47
    关注

    If you want to show an alert message and redirect to another page, you can use JavaScript like this:

    Befor (Your Original Code):

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

    After:

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

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决