标头重定向无法正常工作

时间:2014-08-11 17:05:16

标签: php html redirect

我有以下代码,可以让用户上传文件。这将被写入SQL数据库,然后我希望页面为redirect。谁能告诉我它为什么不重定向?它完成其他行很好。

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
Choose your file:
<input name="csv" type="file" id="csv"/>
<input type="submit" name="Submit" value="Submit" class="button"/> 
</form> 

<?php
$connect = mysql_connect("localhost",username,password); 
mysql_select_db($database,$connect);

if ($_FILES[csv][size] > 0) 
{ 
$file = $_FILES[csv][tmp_name]; 
$handle = fopen($file,"r"); 

while ($data = fgetcsv($handle,1000,",","'")) 
{ 
    if ($data[0]) 
    { 
    $insert_query = "REPLACE INTO `tableNames` SET 
    `schoolName` = '".$schoolname."',
    `teacherName` = '".$data[0]."'
    ;";  

        $result = mysql_query($insert_query);
    } 
} 
} 

header("Location: https://redirecting address/");
?> 

更新

这有效:

....
        $result = mysql_query($insert_query);
    } 
} 
} 

echo("<script>location.href = 'http"//redirect address';</script>");
?> 

3 个答案:

答案 0 :(得分:1)

在您输出任何内容之后,您无法发送标题...如果您希望在写入数据后重定向,请将PHP移至脚本顶部。

答案 1 :(得分:0)

来自Here它说

  

请记住,在任何实际输出之前必须调用header()   通过普通HTML标记,文件中的空行或PHP

发送

如果您想在输出后重定向,请查看Here

答案 2 :(得分:-2)

试试这个:

<?php
$connect = mysql_connect("localhost",username,password); 
mysql_select_db($database,$connect);

if ($_FILES[csv][size] > 0) 
{ 
    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r"); 

    while (!feof($handle)) 
    { 
        $data = fgetcsv($handle,1000,",","'");
        if ($data[0]) 
        { 
            $insert_query = "REPLACE INTO `tableNames` SET 
            `schoolName` = '".$schoolname."',
            `teacherName` = '".$data[0]."'
            ;";  

            $result = mysql_query($insert_query);
        } 
    } 
    header("Location: https://redirecting address/");
    exit;
} 


?> 
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
Choose your file:
<input name="csv" type="file" id="csv"/>
<input type="submit" name="Submit" value="Submit" class="button"/> 
</form>