dpe77294 2017-03-10 23:12
浏览 60
已采纳

如何使用PHP从Web界面中的表单写入现有CSV文件?

I am trying to write to a CSV file using my form from my website, but it's not writing anything to it but instead just making me upload another CSV file.

My code:

The form code:

<form action="" method="post">
    <fieldset style="float:left; text-align: left">

        <legend>Add Users</legend>
        <label for="fn">
            First Name: <input type="text" id="fn" name="fn" onkeypress="fill()" required="required" placeholder="First Name" size="20"/>
        </label>
        <label for="ln" >
            Last Name: <input type="text" id="ln" name="ln" onkeyup="fill()" required="required" placeholder="Last Name" size="20"/>
        </label>

        <br/>

        <label for="full">
            Full Name: <input type="box" id="full" name="full" required="required" placeholder="Full Name" size="54"/>
        </label>

        <br/>

        <label for="log">
            User Logon: <input type="text" id="log" name="log" onkeypress="fill2()" required="required" placeholder="User Logon" size="27"/>
        </label>
        <select>
            <option value="test">
                test
            </option>
        </select>

        <br/>

        <label for="log2">
            User logon name (pre-Windows 2003): <select>
            <option value="test\">
                test\
            </option>
        </select><input type="text" id="log2" name="log2" required="required" placeholder="Username" size="20"/>
    </label>

    <br/>

    <label for="pwd">
        Password: <input type="password" id="pwd" name="pwd" required="required" placeholder="Password" size="20"/>
    </label>
    <label for="cpwd">
        Confirm Password: <input type="password" id="pwd" name="cpwd" required="required" placeholder="Password" size="20"/>
    </label>
    <p for="1st" style="font-size: 16px"><input type="checkbox" id="1st" name="1st" value="1st"/>User must change password at next logon</p>
    <p for="2nd" style="font-size: 16px"><input type="checkbox" id="2nd" name="2nd" value="2nd"/>User cannot change password</p>
    <p for="3rd" style="font-size: 16px"><input type="checkbox" id="3rd" name="3rd" value="3rd"/>Password never expires</p>
    <p for="4th" style="font-size: 16px"><input type="checkbox" id="4th" name="4th" value="4th"/>Account is disabled</p>

    <input type='submit' name='submit' value='Add User'>
    <button type="reset" value="Clear" onclick="" id="auser">Clear</button>

</fieldset>
</form>

The php code:

if(isset($_POST['submit']))
{
    //collect form data
    $fn = $_POST['fn'];
    $ln = $_POST['ln'];
    $full = $_POST['full'];
    $log = $_POST['log'];
    $log2 = $_POST['log2'];
    $pwd = $_POST['pwd'];

    //if no errors carry on
    if(!isset($error))
    {
        # Title of the CSV
        $title   = "First Name, Last Name, Full Name, User Logon, User Logon(pre-2003), Password,
";
        $space = "
";
        //set the data of the CSV
        $Content = "$fn, $ln, $full, $log, $log2, $pwd
";

        # set the file name and create CSV file
        $FileName = ("adduser.csv");
        header('Content-Type: application/csv'); 
        header('Content-Disposition: attachment; filename="' . $FileName . '"'); 
        echo $title;
        echo $space;
        echo $Content;
        exit();
    }
}

What I need is that, when I press add user on the form, it will add that user to the adduser.csv file, but right now its just, like I said, making me download a CSV file, and not writing to the adduser.csv file.

  • 写回答

1条回答 默认 最新

  • duanhuang1967 2017-03-10 23:39
    关注

    You can use the FILE_APPEND flag with file_put_contents to append your data to the file.

    if(!isset($error))
    {
        $filename = "adduser.csv";
    
        // create the file if it doesn't exist
        $column_headings = "First Name, Last Name, Full Name, User Logon, User Logon(pre-2003), Password
    ";
    
        if (!file_exists($filename)) {
            file_put_contents($filename, $column_headings);
        }
    
        $Content = "$fn, $ln, $full, $log, $log2, $pwd
    ";
    
        // append the content to the file
        file_put_contents($filename, $Content, FILE_APPEND);
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。