0

I have the following code that enables the user to upload a file. This gets written into a SQL database and I then want the page to redirect. Can anyone tell me why it is not redirecting? It completes the other lines fine.

<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/");
?> 

UPDATE

This worked:

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

echo("<script>location.href = 'http"//redirect address';</script>");
?> 
RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • Place your form / HTML below your PHP. Using > http://php.net/manual/en/function.error-reporting.php would've shown you an error. Since you're most likely not checking for errors and/or reporting is not ON. Had it been on or checking for it, would've thrown you something like `Warning: Cannot modify header information - headers already sent by...` - *right?* - Knew it ;) – Funk Forty Niner Aug 11 '14 at 17:08
  • If you absolutely MUST have the form in its present position, use a meta refresh or JS method instead of the header, as shown in the duplicate question's solutions. – Funk Forty Niner Aug 11 '14 at 17:14
  • @Fred-ii- I am confused - you say move it and then keep it where it is. Bt you are right. I have looked at he log and have "Cannot modify header information - headers already sent by " – RGriffiths Aug 11 '14 at 17:24
  • I gave you an alternate solution saying **IF** you have to keep it the way it is. Sometimes, people cannot modify the way a file is setup; am only stating possibilities and there stands to be many, believe me. – Funk Forty Niner Aug 11 '14 at 17:26
  • I noticed your comment about it not working. Make sure there are no spaces before your opening ` – Funk Forty Niner Aug 11 '14 at 17:31
  • 1
    Thanks for your help. Fixed as shown above. – RGriffiths Aug 11 '14 at 18:10
  • You're welcome Richard; glad it got resolved. Was it the byte order mark after all? Curious. – Funk Forty Niner Aug 11 '14 at 18:14

3 Answers3

1

You cannot send headers AFTER you've output anything... move the PHP to the top of your script if you want it to redirect after writing data.

Ben D
  • 14,321
  • 3
  • 45
  • 59
0

From Here it says

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

Take a look Here if you're looking to redirect after output.

Bijan
  • 7,737
  • 18
  • 89
  • 149
-2

try this:

<?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> 
Mikerizzo
  • 587
  • 1
  • 4
  • 22