导出查询结果

时间:2012-01-12 01:25:28

标签: php mysql html

我的程序运行方式有问题。情况是我有一个表格要求提供日期,然后当点击按钮提交时,将显示具有相同日期的记录。之后有一个按钮提取,如果我点击它,结果将导出为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 个答案:

答案 0 :(得分:1)

我真的不明白这个问题,但我看到了一些问题:

  • 我没有看到任何文件写入。
  • 您的$out变量会获得按钮的值$_POST['extract'],该值始终为extract,仅此而已。

答案 1 :(得分:0)

您必须检查为PHP启用设置的Web服务器。将页面本身作为内容获取的一种方法是在服务器未设置为运行PHP时。然后服务器会提示下载页面而不是运行/执行它。

在解决代码问题之前,请确保您的Web服务器可以正确地提供PHP脚本或文件。

接下来看一下如何存储包含文件的给定代码并检查其扩展名是否为.php。

然后检查您在主页中如何包含该页面或如何访问该页面。