php将数据导出到csv

时间:2014-04-18 00:29:06

标签: php web-applications

我需要一个帮助,点击导出按钮导出搜索查询。我能够从数据库中获取数据到浏览器。但是,当我单击导出按钮时,它会刷新页面,并且不执行任何操作。

我是php和编程的新手。任何帮助将不胜感激:)

    <?php
    include('dbcon.php');

    ?>
    <form id="searchform" action="index.php" method="post">
    <table>
        <tr>
            <td class="searchleftcol"><h3>Service:</h3></td>
        <td>
             <select id="service" name="service" class="searchoption">
             <option value="">-- Select Service Name --</option>
             <?php
             $resultservice = mysqli_query($con,"Select * from services") ?>
             <?php
             while ($line = mysqli_fetch_array($resultservice)) {
             ?>

             <option value="<?php echo $line['serviceid'];?>"> <?php echo $line['service'];?> </option>

            <?php
            }
            ?>
            </select>

        </td>
              </tr>
              <tr>
                <td>
                    <h3>Environment:</h3>
                </td>
                <td>
                    <select id="environment" name="environment" class="searchoption">
                    <option value="">-- Select Environment --</option>
                    <?php
                    $resultdomain = mysqli_query($con,"Select * from evn") ?>
                    <?php
                    while ($line = mysqli_fetch_array($resultdomain)) {
                    ?>

                    <option value="<?php echo $line['envid'];?>"> <?php echo $line['env'];?> </option>

                    <?php
                    }
                    ?>
                    </select>       

                </td>
              </tr>
              <tr>
                <td>
                    <h3>Status:</h3>
                </td>
                <td>
                    <select name="status" class="searchoption">
                    <option value="Active">Active</option>
                    <option value="Inactive">Inactive</option>
                    </select>

                </td>
              </tr>
            </table>
                        <input type="reset" name="reset">
                        <input type="submit" name="submit" value="Search">
                        <input type="submit" name="export" value="Export" />
                        </ul>
    </form>


    <?php

    if (isset($_POST['submit'])) {

    if (empty($_POST['service'])) {

        echo "Please select service in dropdown" . "</br>";

    }

    else {

        $service = $_POST['service'];

    }

    if (empty($_POST['environment'])) {

        echo "Please select Environment in dropdown" . "</br>"; 

    }

    else {

        $env = $_POST['environment'];

    }

    if ((!empty($service)) && (!empty($env))) { 

    $sql="SELECT * from servers"; 

    if (!mysqli_query($con,$sql)) {
    die('Error: ' . mysqli_error($con));
    }

    $mydata = mysqli_query($con,$sql);
    $rowcount = mysqli_num_rows($mydata);

//这里我删除了显示MySQL数据的代码。

    if (isset($_POST['export'])) {

    if (empty($_POST['service'])) {

        echo "Please select service in dropdown" . "</br>";

    }

    else {

        $service = $_POST['service'];

    }

    if (empty($_POST['environment'])) {

        echo "Please select Environment in dropdown" . "</br>"; 

    }

    else {

        $env = $_POST['environment'];

    }

    if ((!empty($service)) && (!empty($env))) { 

    $sql="SELECT * from servers";

    if (!mysqli_query($con,$sql)) {
    die('Error: ' . mysqli_error($con));
    }

    $mydata = mysqli_query($con,$sql);
    $rowcount = mysqli_num_rows($mydata);

        //Programetically get the Headings of the excel columns
        $columns_total = mysqli_num_fields($sql);
        for ($i = 0; $i < $columns_total; $i++) {
            $heading = mysql_field_name($sql, $i);
            $contents .= '"'.$heading.'",';
        }
        $contents .="\n";

        // Get Records from the table
        while ($row = mysqli_fetch_array($sql)) {
        for ($i = 0; $i < $columns_total; $i++) {
        $contents.='"'.$row["$i"].'",';
        }
        $contents.="\n";
        }

        // Remove html and php tags etc.
        $contents = strip_tags($contents);

        //header to make force download the file
        Header("Content-Disposition: attachment; filename=ProductsReport".date('d-m-Y').".csv");

        print $contents;

    }
    }

    mysqli_close($con);
    }

    ?>}

谢谢,

0 个答案:

没有答案