我有一个可以更新数据库内部信息的表单,一切都正常工作......除了由于某种原因标题对我不起作用。我在同一站点内的许多其他脚本中使用 header 重定向;正是这个脚本导致 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();
?>
最佳答案
在发送任何实际输出之前,必须调用 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; } ?>
关于PHP header 重定向在表单提交时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31370228/