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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器