PHP标头重定向不在表单提交上工作

时间:2015-07-12 16:52:32

标签: php mysql forms redirect header

我有一个更新数据库内部信息的表单,一切都完美无缺,因为它应该.......除了某些原因标题不适合我。我在同一站点的许多其他脚本中使用头重定向;只有这一个脚本,标题重定向不起作用。我知道标题在其中的if语句被调用,因为标题下方的回显显示在页面上,它只是忽略标题行。如果您有任何建议,将非常有帮助。

下面的代码已经被最小化,所以每个人都可以阅读但是如果它仍然是一个问题,那就更糟糕了。

表格

<form class="editUser" action="uploadEmployee.php" method="post" enctype="multipart/form-data" />
    <input name="editUserFirstName" id="editUserFirstName" type="text" placeholder="Enter The User's First Name" />
    <label>Employee's Coverage</label>
    <input type="file" name="file_array[]" placeholder="Add The Employees Coverage" />
    <input name="addUserSubmit" type="submit" value="Submit" />
</form>

上传脚本

<?php

include("includes/connect.inc.php");

if(isset($_FILES['file_array'])){

    $day = date('d');
    $month = date('m');
    $dateObj   = DateTime::createFromFormat('!m', $month);
    $monthName = $dateObj->format('F'); // March
    $year = date('Y');
    $date = $monthName . ", " . $day . ", " . $year;
    $name_array = $_FILES['file_array']['name'];
    $tmp_name_array = $_FILES['file_array']['tmp_name'];
    $type_array = $_FILES['file_array']['type'];
    $size_array = $_FILES['file_array']['size'];
    $error_array = $_FILES['file_array']['error'];
    $payCheque  = $name_array[0];
    $T4         = $name_array[1];
    $coverage   = $name_array[2];
    $selectedId = $_POST['editId'];
    $name       = $_POST['editUserFirstName'];

    if($_POST['editUserPermission']){
      $permission = "1";
    }else{
      $permission = "0";
    }


    for($i = 0; $i < count($tmp_name_array); $i++){

        if(move_uploaded_file($tmp_name_array[$i], "uploads/".$name_array[$i])){

             $title = "This Is A Test Title";
             $icon = "0";
             if($i == 0){
             $icon = "1";
             $title = "You Have Received A New Pay Cheque";
             $comment = "Hello " .  $name . ", click view document to view and download your pay cheque for " . $date . ". Your pay cheque will be a PDF file. Thank you.";
              }

             $sql = "INSERT INTO securedFiles (title, date, PDF, comment, idOfUser, icon)
              VALUES ('$title', '$date', '$name_array[$i]', '$comment', '$documentId', '$icon')";

              if ($connect->query($sql) === TRUE) {
                //header("Location: hub.php");
              } else {
                  echo "Error: " . $sql . "<br>" . $connect->error;
              }

              echo $payCheque ." upload is complete<br>";

        } else {
            echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
        }
    }

   $sql = "UPDATE dealerEmployees SET firstName = '$_POST[editUserFirstName]', lastName = '$_POST[editUserLastName]', password = '$_POST[editUserPW]', permission = '$permission', address = '$_POST[editUserAddress]', email = '$_POST[editUserEmail]', phone = '$_POST[editUserPhone]' WHERE id = $selectedId";

    if ($connect->query($sql) === TRUE) {
     header('Location: hub.php');
     echo "Success";
    } else {
        echo "Error: " . $sql . "<br>" . $connect->error;
    }    

}


 //$connect->close();


?>

1 个答案:

答案 0 :(得分:2)

必须在发送任何实际输出之前调用

header(),无论是普通HTML标记,文件中的空行还是PHP。

在你的情况下,你在header()之前使用echo

所以你可以使用重定向方法(我在所有项目中使用,没有问题)


<?php


if ($connect->query($sql) === TRUE) {
    echo "<script> parent.self.location = \"hub.php\";</script>";
     echo "Success";
    } else {
         echo "Error: " . $sql . "<br>" . $connect->error;
    }    

?>