2

I have a form which updates information inside of a DB, everything works perfectly as it should....... except for some reason the header does not work for me. I am using the header redirect in a lot of other scripts within the same site; it is just this one script that the header redirect does not work. I know that the if statement the header is inside of is called because the echo right below the header is displayed on the page, its just kind of ignoring the header line. If you have any suggestions that would be very helpful.

The code below has been minimized so its not to long for everyone to read but if it is still a problem ill clean it up even more

the form

<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>

The uploading scripts

<?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();


?>
2
  • Dont pass user input direct to SQL query. SQL injections are possible this way, use prepared statements. Commented Jul 12, 2015 at 17:16
  • ok thank you for the advice, I will defiently look into fixing that Commented Jul 12, 2015 at 17:18

1 Answer 1

2

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

And in your case, you are using echo before header()

So you can use a redirect method(that i use in all my projects, no problems ever)

<?php


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

?>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.