【问题标题】:Write HTML form input to csv file with PHP使用 PHP 将 HTML 表单输入写入 csv 文件
【发布时间】:2015-03-15 01:11:25
【问题描述】:

我有一个 PHP 表单,用户可以在其中输入他们的数据。提交时,它应该打开一个现有的 CSV 文件并附加一些数据。我已经尝试了很多东西,但这是我目前正在使用的最新代码:

HTML:

<html>
<head><title>Design Request</title></head>
<title>Design Request</title>     

  <form action="test2.php" method="POST">
    <fieldset align="left">
    <legend>Executive Sponsor Form</legend>
    <p><i>To be completed by the executive sponsor</i></p>
    <table>
      <tr>
        <td>Name:</td><td><input type="text" name="SPONSOR_NAME" size="40" /></td>
      </tr>
      <tr>
        <td>Position:</td><td><input type="text" name="SPONSOR_POSITION" size="40" /></td>
      </tr>
      <tr>
        <td>Telephone Number:</td><td><input type="text" name="SPONSOR_TELEPHONE_NUMBER" size="40" maxlength="11" /></td>
      </tr>
      <tr>
        <td>Email Address:</td><td><input type="email" name="SPONSOR_EMAIL" size="40" /></td>
      </tr>
      <tr>
        <td>Budget :</td><td><input type="checkbox" /><input type="text" name="BUDGET" size="37" maxlength="6" placeholder="budget code" /></td>
      </tr>
      <tr>
        <td>Aligned to which<br> priority:</td><td><input type="text" name="PRIORITIES" size="40" /></td>
      </tr>
      <tr>
        <td> Benefit to Business:</td><td><textarea cols="42" rows="10" name="BENEFIT"></textarea></td>
      </tr>
    </form>
    <input type="submit" value="Submit">

PHP:

<html>
  <head>
    <title>Your (Ref No: ["SPONSOR_NAME"]) has been submitted.</title>        
  </head>
  <?php
    $data = array("" . ',' . "SPONSOR_NAME" . ',' . "" . ',' . "" . ',' . "SPONSOR_EMAIL" . ',' . "PRIORITIES" . );
    $cr;

    $fp = fopen("/var/www/testdb.csv","w");
    foreach ($data as $fields){
      fputcsv($fp, $fields);
    }
    fclose($fp);
  ?>
</html>

有些字段是在一个工作表上输入的,而有些是在另一个工作表上输入的,这就是为什么我有一些空字段的数组(我想要列 BE 和 F 中的数据),其他列将包含来自另一个表单的数据。

【问题讨论】:

  • 帮助 John 并使用适当的数据库!
  • 它是 excel 中现有的一个

标签: php html forms csv web-applications


【解决方案1】:

由于您已经为数据构建了数组,您可以使用 PHP 的 implode() 函数将行写入文件

  $data = array("" . ',' . "SPONSOR_NAME" . ',' . "" . ',' . "" . ',' . "SPONSOR_EMAIL" . ',' . "PRIORITIES" . );

  $file = "/var/www/testdb.csv";          //file to append to
  $current = file_get_contents($file);    //open the file
  $current .= "\n" . implode(",",$data);  //add line-break then comma separated array data
  file_put_contents($file, $current);     //append new content to file

如果您需要从另一个表单中填写数据中的这些空白,则必须重新打开文件,提取最后一行数据,explode() 将其放入一个数组,填写缺失的数据,然后覆盖csv 中的原始行...我不建议这样做,因为其他用户可以提交表单的第一部分,然后将他们的新数据附加到 csv 的不正确行...在这种情况下,您应该真正接受建议@symcbean 并使用适当的数据库。

【讨论】:

  • 我不能有类似的东西:打开文件,表格 1 转到 b 列(发布数据)转到 e(发布数据)转到 f(发布数据)。然后表格 2 转到 a 列(发布数据)转到 c 列(发布数据)等?
  • PHP 不会将 CSV 文件中的数据视为具有列。每次要写入多维数组时,都必须解析 CSV 数据,将新数据写入数组,然后将数组重新保存到 CSV。这对于小型应用程序来说还不错,但是会出现大规模的性能问题以及并发进程相互覆盖的问题。 -- 读取数据库可以优雅轻松地处理这些问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-08
  • 2019-03-23
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
相关资源
最近更新 更多