为什么在提取到csv文件时会出现空白页?

时间:2014-01-14 08:11:06

标签: php csv if-statement extract

我的代码工作得非常好但是当我添加if..else时,它会输出一个空白的html页面而不是将其解压缩到csv文件。

我所做的是让用户在下拉列表中选择一个选项。然后我将根据用户选择的内容编写不同的csv格式。我首先尝试了然后这发生了。谁能告诉我我做错了什么?

这是我的代码:

if(isset($_POST['extract'])){
    error_reporting(E_ERROR | E_PARSE);
    $dropdownvalue = $_POST['ship'];
    if($dropdownvalue == 'normal'){  
        $name = "sample";
        $file = $name.".csv";
        $con = mysql_connect("localhost", "root");
        if(!$con){
            echo "Error connection";
        }

        $select_db = mysql_select_db('outbound', $con);
        if(!$select_db){
            echo "Error to select database";
        }
        mysql_set_charset("utf8", $con);

        header("Content-type: text/csv; charset=UTF-8");
        header('Content-disposition: attachment; filename='.basename($file)); 

        $myquery = mysql_query("SELECT * FROM temp");

        //While loop to fetch the records
        $contents = "Name, Company, Address1\n";
        while($row = mysql_fetch_array($myquery))
        {
            $contents.='"'.$row['0'].'"'.",";
            $contents.='"'.$row['1'].'"'.",";
            $contents.='"'.$row['2'].'"'."\n";  
        }
        $contents_final = chr(255).chr(254).mb_convert_encoding($contents, "UTF-16LE","UTF-8");
        print $contents_final;
    }
}

我添加的是:

$dropdownvalue = $_POST['ship'];
if($dropdownvalue == 'normal'){ 
I PASTE ALL THE CODE INSIDE
}

在我添加之前,代码工作正常。

这是下拉列表:

<form method="POST" action="mycsv.php">
Shipping Provider: <select id='ship' name='ship'>
<option value="normal">NORMAL</option>
<option value="val2">Value2</option>
</select>

//Button where displays datas inside the table. Not included in the problem    
<input type='button' onclick='ajaxFunction()' 
                          value='Add to Listview'/>  
//Button to extract to csv file 
<input type="submit" value="Extract CSV file" name="extract">
</form>

0 个答案:

没有答案