如何插入多个具有相同名称的输入值在php中插入csv

时间:2014-09-01 09:17:16

标签: php html forms csv

这是具有多个具有相同名称的输入值的表单数据:

<form id="form1" name="form1" method="post" action="insert3.php">
    <table border=1>
         <tr><th><span>Outage Reasons ( Date )</span></th> <th> RNA - CP ( Radio Network Availability - Customer Perceived) </th> <th> RNA - PW ( Radio Network Availability - after removing Planned Work minutes) </th> </tr>
        <tr><td><input type="text" name="outage[]" id="outage" /></td><td><input name="rna_cp[]" type="text" id="rna_cp" size="50" /></td><td><input name="rna_pw[]" type="text" id="rna_pw" size=50/></td></tr>
        <tr><td><input type="text" name="outage[]" id="outage" /></td><td><input name="rna_cp[]" type="text" id="rna_cp" size="50" /></td><td><input name="rna_pw[]" type="text" id="rna_pw" size=50/></td></tr>
        <tr><td><input type="text" name="outage[]" id="outage" /></td><td><input name="rna_cp[]" type="text" id="rna_cp" size="50" /></td><td><input name="rna_pw[]" type="text" id="rna_pw" size=50/></td></tr>
        <tr><td><input type="text" name="outage[]" id="outage" /></td><td><input name="rna_cp[]" type="text" id="rna_cp" size="50" /></td><td><input name="rna_pw[]" type="text" id="rna_pw" size=50/></td></tr>
        <tr><td><input type="text" name="outage[]" id="outage" /></td><td><input name="rna_cp[]" type="text" id="rna_cp" size="50" /></td><td><input name="rna_pw[]" type="text" id="rna_pw" size=50/></td></tr>
        <tr><td><input type="text" name="outage[]" id="outage" /></td><td><input name="rna_cp[]" type="text" id="rna_cp" size="50" /></td><td><input name="rna_pw[]" type="text" id="rna_pw" size=50/></td></tr>
        <tr><td><input type="text" name="outage[]" id="outage" /></td><td><input name="rna_cp[]" type="text" id="rna_cp" size="50" /></td><td><input name="rna_pw[]" type="text" id="rna_pw" size=50/></td></tr>
        </tr>
        <tr><td></td>
            <div align="center">
            <td><input type="submit" name="Submit" id="Submit" value="Submit" />
            <input type="reset" name="Reset" id="button" value="Reset" />
            </td>
            </div>
        </tr>
    </table>
    </form>

这是插入csv文件的PHP脚本:

<?php

$outage=$_POST['outage'];
$rna_cp=$_POST['rna_cp'];
$rna_pw=$_POST['rna_pw'];

for($i=0;$i<count($_POST['outage']);$i++)
{
    $list = array (
    array("$_POST['outage']", "$_POST['rna_cp']", "$_POST['rna_cp']" )
  );

}

$fp = fopen('file.csv', 'a');

foreach ($list as $fields) {

    fputcsv($fp, $fields."\r\n");
}

fclose($fp);

?>

但是数据没有插入文件,错误是:

解析错误:语法错误,意外T_ENCAPSED_AND_WHITESPACE,期待T_STRING或T_VARIABLE或T_NUM_STRING

感谢。

2 个答案:

答案 0 :(得分:1)

试试这个......

<?php

$list = array();

for($i=0;$i<count($_POST['outage']);$i++)
{
    $list[$i] = array($_POST['outage'][$i], $_POST['rna_cp'][$i], $_POST['rna_pw'][$i] );

}

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);

?>

答案 1 :(得分:0)

我尝试在您的代码中调试$list的初始化:

<?php

$list = array(); // <- declare array first
for($i=0; $i<count($_POST['outage']); $i++)
  $list[] = array($_POST['outage'][$i], $_POST['rna_cp'][$i], 
                  $_POST['rna_pw'][$i]); 
  // ^- push_back into array. 
  //  - $_POST['outage'] is array too, 
  //    so you should use [] operator. 
  //  - Use "" for strings only, not for normal variables.

修改 这对我有用:

/*
// test input
$_POST['outage'] = ['a0', 'b0', 'c0'];
$_POST['rna_cp'] = ['a1', 'b1', 'c1'];
$_POST['rna_pw'] = ['a2', 'b2', 'c2'];
*/
$fp = fopen('file.csv', 'a');
for($i=0; $i < count($_POST['outage']); ++$i) {
  $fields = array($_POST['outage'][$i], $_POST['rna_cp'][$i], 
                  $_POST['rna_pw'][$i]);
  fputcsv($fp, $fields);          
}
fclose($fp);