0

我将以下数据重复了五次,并希望返回数组并将数组写入我的 csv 文件:

    <strong>Inventory number:</strong>
    <input name="inv[]" type="text" size="10" id="inv[]"/>
    <strong>Description:</strong>
    <input name="des[]" type="text" size="30"id="des[]"/> 

    <strong>Inventory number:</strong>
    <input name="inv[]" type="text" size="10" id="inv[]"/>
    <strong>Description:</strong>
    <input name="des[]" type="text" size="30"id="des[]"/> 

我做了一个foreach:

for ($i = 0, $rowcount = count($_POST['inv']); $i < $rowcount; $i++)
    {
$inv = $_POST['inv'][$i];   // get inventory
$des  = $_POST['des'][$i]; // get description
      }

然后有一个写命令:

if(empty($errorMessage))
{
    $fs = fopen("data.csv","a") or die("Unable to open file for output");
    fwrite($fs,$sn . ", " . $givenname . ", " . $mail . ", " . $uid . ", " . $inv . ", " . $des . ", " . date("Y.m.d H:i:s") . "\n");
    fclose($fs);

    $destination_url = "receipt.php";

    header("Location: $destination_url");
    exit;
}

但它只将最后一个数组写入 csv 文件。

你能帮我吗?谢谢

4

1 回答 1

1

您必须遍历数组并将每条记录写入文件:

<?php
if(empty($errorMessage))
{
    $rowcount = count($_POST['inv']);
    $fs = fopen("data.csv","a") or die("Unable to open file for output");
    for ($i = 0; $i < $rowcount; $i++)
    {
        $inv = $_POST['inv'][$i];   // get inventory
        $des  = $_POST['des'][$i]; // get description
        fwrite($fs, $sn . ", " . $givenname . ", " . $mail . ", " . $uid . ", " . 
            $inv . ", " . $des . ", " . date("Y.m.d H:i:s") . "\n");
    }

    fclose($fs);
}

$destination_url = "receipt.php";
header("Location: $destination_url");
于 2013-06-28T07:54:15.097 回答