2012-01-12 162 views
0

我的程序運行方式有問題。情況是我有一個表單要求日期,然後點擊按鈕提交時,將顯示具有相同日期的記錄。之後,有一個按鈕提取,如果我點擊它,結果將導出到CSV文件。導出查詢結果

這是我的代碼:

<html> 
     <h1> TC/GC Per Sales Frequency Bracket </h1> 
      <body> 
      <?php 
       $df = $_POST['df']; 
       $dt = $_POST['dt']; 
       $db = 'mds_reports'; 

       if($connect = mysql_connect("172.16.8.32", "mds_reports", "password")) 
       $connect = mysql_select_db($db); 
        else die ("Unable to connect".mysql_error()); 

       echo '<form action = "TCFrequencyBracket.php" method = "post">'; 
       echo '<table>'; 

       echo '<tr><td>Date From :</td><td><input name = "df" type = "text" /></td></tr>'; 
       echo '<tr><td>Date To :</td><td><input name = "dt" type = "text" /></td></tr>'; 
       echo '<tr><td><input name = "submit" type = "submit" id = "submit" value = "submit"/></td></tr>'; 
       echo '</table>'; 
       echo '</div>'; 
       echo '</div>'; 
       echo '</form>'; 

       if (isset($_POST['submit'])) 
       { 
        echo '<table border = "1" cellspacing = "1">'; 
        echo '<th>Restaurant Code</th>'; 
        echo '<th>Restaurant Name</th>'; 
        echo '<th>100 & BELOW</th>'; 
        echo '<th>101-200</th>'; 
        echo '<th>201-300</th>'; 
        echo '<th>301-400</th>'; 
        echo '<th>401-500</th>'; 
        echo '<th>501-600</th>'; 
        echo '<th>601-700</th>'; 
        echo '<th>701-800</th>'; 
        echo '<th>801-900</th>'; 
        echo '<th>901-1000</th>'; 
        echo '<th>1001 & ABOVE</th>'; 
        echo '<th>TOTAL TC</th>'; 

        $sql = "SELECT restaurant_master.code, restaurant_master.name, 

           COUNT(CASE WHEN mds_orders.GrossTotal < '100' 
            THEN 1 END), 

           COUNT(CASE WHEN mds_orders.GrossTotal >= '100' AND mds_orders.GrossTotal < '200' 
            THEN 1 END), 

           COUNT(CASE WHEN mds_orders.GrossTotal >= '200' AND mds_orders.GrossTotal < '300' 
            THEN 1 END), 

           COUNT(CASE WHEN mds_orders.GrossTotal >= '300' AND mds_orders.GrossTotal < '400' 
            THEN 1 END), 

           COUNT(CASE WHEN mds_orders.GrossTotal >= '400' AND mds_orders.GrossTotal < '500' 
            THEN 1 END), 

           COUNT(CASE WHEN mds_orders.GrossTotal >= '500' AND mds_orders.GrossTotal < '600' 
            THEN 1 END), 

           COUNT(CASE WHEN mds_orders.GrossTotal >= '600' AND mds_orders.GrossTotal < '700' 
            THEN 1 END), 

           COUNT(CASE WHEN mds_orders.GrossTotal >= '700' AND mds_orders.GrossTotal < '800' 
            THEN 1 END), 

           COUNT(CASE WHEN mds_orders.GrossTotal >= '800' AND mds_orders.GrossTotal < '900' 
            THEN 1 END), 

           COUNT(CASE WHEN mds_orders.GrossTotal >= '900' AND mds_orders.GrossTotal < '1000' 
            THEN 1 END), 

           COUNT(CASE WHEN mds_orders.GrossTotal >= '1000' 
            THEN 1 END), 

           COUNT(mds_orders.GrossTotal) 

           FROM mds_orders 
            JOIN restaurant_master 
             ON mds_orders.RestaurantID = restaurant_master.PKID 

           WHERE mds_orders.OrderDate BETWEEN '$df' AND '$dt' 
             AND mds_orders.StatusFKID = 2 

           GROUP BY restaurant_master.code, restaurant_master.name"; 
        // echo $sql; 

        echo 'Date select from ' . $df; 
        echo ' to ' . $dt; 
        echo '<br /><br />'; 

        $result = mysql_query($sql) 
           or die("SELECT Error: ".mysql_error()); 
        $num_rows = mysql_num_rows($result); 
        print "There are $num_rows records.<br>"; 
        echo '<br />'; 

        while ($get_info = mysql_fetch_row($result)){ 
        print "<tr>\n"; 
        foreach ($get_info as $fields) 
        print "\t<td>$fields</td>\n"; 
        print "</tr>\n"; 
        } 
        echo '</table>'; 

       echo '<form>'; 
       echo '<div>'; 
       echo '</div>'; 
       echo '<table>'; 
       echo '<br><input name = "extract" type = "submit" id = "extract" value = "extract"/></td><td></br></br>'; 
       echo '<tr><td></table></td></td>'; 
       echo '</form>'; 

        $out = ''; 

        if (isset($_POST['extract'])) { 
        $out .= $_POST['extract']; 
        } 

        $filename = $file."_".date("Y-m-d_H-i",time()); 

        print stripslashes($out); 

        exit; 


       } 
      ?> 
     </body> 
</html>` 

現在的錯誤是,當我嘗試打開即使主頁面沒有顯示已顯示一個提示程序。而且文件的內容是代碼而不是結果。

+2

我無法看到如何顯示下載提示。您不會強制下載,您不會輸出任何非HTML mime類型,而只是輸出html,句點。 – 2012-01-12 01:41:20

+1

除了代碼沒有做你想要的/期望的,這個代碼有一些設計和實現問題(例如使用過時的mysql擴展,[SQL注入](http://unixwiz.net/techtips/sql-injection .html)漏洞,在輸出HTML時使用['die'](http://www.phpfreaks.com/blog/or-die-must-die),混合數據訪問和輸出)。考慮發佈在[codereview.SE](http://codereview.stackexchange.com/)上,儘管你真的需要閱讀一個好的[PHP開發指南](http://stackoverflow.com/questions/2119083/) 。 – outis 2012-01-12 01:47:16

回答

1

我真的不明白的問題,但有幾個問題,我看到:

  • 我沒有看到任何書面文件。
  • 您的$out變量獲取您的按鈕的值,$_POST['extract'],總是extract,僅此而已。
0

您必須檢查設置爲啓用PHP的Web服務器。將服務器設置爲運行PHP時,獲取頁面本身的一種方式是內容。然後服務器確實會提示下載頁面而不是運行/執行它。

在解決您的代碼問題之前,請確保您的Web服務器可以正確地提供PHP腳本或文件。

接下來看看你如何存儲給定的代碼包含文件,並檢查其擴展名是否.php。

然後檢查您是如何將該頁面包含在主頁面中的,或者是如何訪問該頁面的。