drhzn3911 2018-01-30 05:29
浏览 31
已采纳

PHP没有将MySQL写入CSV

I am trying to have my mysql database that is displayed on my webpage be exported to a csv on a button click, however it is not functioning as expected. I am attempting to do the following on the click of the export button, but when you click the Export CSV button it goes to a page "url/export.php" and gives an "error page does not exist webpage"

export button definition:

     <div class="topnav">
<form method="post" action="export.php">
  <input type="submit" name="export" value="CSV Export" class="btn btn-i"
  <a class="active" href="#export">Export to CSV</a>
</form>
  <div class="search-container">
    <form action="/action_page.php">
      <input type="text" placeholder="Search.." name="search">
      <button type="submit"><i class="fa fa-search"></i></button>
    </form>
  </div>
</div>

export.php code:

    <?php
//export.php
if(isset($_POST["export"])) {
    $connect = mysqli_connect("localhost", "root", "password", "dbName");
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');
    $output = fopen("php://output", "w");
    fputcsv($output, array('Username', 'Password', 'PhysicianID'));
    $query = "SELECT * FROM Users";
    $result = mysql_query($connect, $query);
    while($row = mysqli_fetch_assoc($result)) {
        fputcsv($output, $row);
    }
    fclose($output);
} 
?>
  • 写回答

1条回答 默认 最新

  • dpps0715 2018-01-30 05:39
    关注

    You are fundamentally misunderstanding how server-side and client-side code interact.

    A PHP script just outputs everything it comes across until a PHP opening tag is come across (<?php). Then code is interpreted as PHP. Processing is done, perhaps some more output is made. After a close PHP tag (?>) things are just output again (subject to flow control structures and such).

    PHP has no idea what Javascript is, and vice versa.

    Your PHP script runs when the page is requested. It outputs an HTML <script> tag, and some Javascript. And at the same time as this HTML is generated, all that code in your <?php delimiters is running on the server. Not on a click event -- right when the page is generated.

    If you want server-side code to run in response to a client-side event, you'll need to make a request (either via Javascript or via a new page fetch) to a server-side script. That script is where this database-access code should be.

    For example, the button might be on an HTML page like this:

    <form action="get-csv.php" method="POST">
        <button class="active">Export to CSV</button>
    </form>
    

    This can be on a plain HTML page, or output by another PHP script.

    Now put your code to export data from the database in get-csv.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助