我在這裏要做的是讓用戶輸入DATEFROM和DATETO。然後,當用戶單擊按鈕解壓縮到Excel文件時,將執行查詢,在該位置將從表中選擇所有列,其中BETWEEN DATEFROM and DATETO
。同時,它會下載一個excel文件,輸出應該在那裏。將用戶輸入的查詢導出到Excel - 只有HTML部分可見
我能夠得到正確的查詢並將其輸出到html中,但我在將它導出到Excel時遇到問題。下面是問題:
即使我不單擊按鈕,導出到Excel仍在工作。當我在本地主機上運行程序時,它會自動導出到Excel文件。
Excel文件內部只有HTML部分。
<form method="POST" action=""> DATE FROM: <input type="date" name="datefrom"> TO: <input type="date" name="dateto"> <input type="submit" value="Extract excel file" name="extract"></input> </form> <?php header("Content-type: text/csv; charset=UTF-8"); header('Content-Disposition: attachment; filename=Export.csv'); if(isset($_POST['extract'])){ $con = mysql_connect("localhost", "root"); if(!$con){ echo "Error connection"; } $select_db = mysql_select_db('sample', $con); if(!$select_db){ echo "Error to select database"; } mysql_set_charset("utf8", $con); $datefrom = $_POST['datefrom']; $dateto = $_POST['dateto']; $myquery = mysql_query("SELECT * FROM biometrics WHERE date_created BETWEEN '" . $datefrom . "' AND '" . $dateto . "' ORDER BY empno"); //While loop to fetch the records $contents = "id_biometrics,empno,date_created,time_created,status\n"; while($row = mysql_fetch_array($myquery)) { $contents.=$row['id_biometrics'].","; $contents.=$row['empno'].","; $contents.=$row['date_created'].","; $contents.=$row['time_created'].","; $contents.=$row['status']."\n"; } $contents_final = chr(255).chr(254).mb_convert_encoding($contents, "UTF-16LE","UTF-8"); print $contents_final; } ?> </body>
*旁註:*停止使用不推薦使用的'mysql_ *'函數。改爲使用[MySQLi](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual/en/book.pdo.php)。 – Raptor