PHP代码写入现有的csv数据库

时间:2016-10-10 14:13:59

标签: php csv

我一直在为工作相关的联系人运行.csv数据库,我不能使用MYSQL等作为其他用户及其运行的系统的有限可访问性。

我有代码写入自己的.csv,每次我点击提交时都会创建它。但我需要它写入现有的.csv文件,只需在每次提交时添加一个新的行/行。无论如何调整下面的代码来修改当前的.csv文件,而不是下载它。

<?php
if(isset($_POST['submit'])){

//collect form data
$no = $_POST['no'];
$rating = $_POST['rating'];
$employee = $_POST['employee'];
$department = $_POST['department'];
$rate = $_POST['rate'];
$county = $_POST['county'];
$number = $_POST['number'];
$email = $_POST['email'];
$focalpointref = $_POST['focalpointref'];
$confirmedon = $_POST['confirmedon'];
$comments = $_POST['comments'];

//check name is set
if($no ==''){
    $error[] = 'Rating Number is required';
}

//check for a valid email address
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
     $error[] = 'Please enter a valid email address';
}

//if no errors carry on
if(!isset($error)){

    # Title of the CSV
    $Content = "No, Rating, Department, Rate, County, Number, Email, FocalpointRef, ConfirmedOn, Comments\n";

    //set the data of the CSV
    $Content .= "$no, $rating, $employee, $rate, $county, $number, $email, $focalpointref, $confirmedon, $comments\n";

    # set the file name and create CSV file
$FileName = "C:\Users\AMelsom\Desktop\salary_book_clean.csv";//"formdata-".date("d-m-y-h:i:s").".csv";
$FileName = "C:\Users\AMelsom\Desktop\salary_book_clean.csv";
file_put_contents($FileName, $Content);
    echo $Content;
    exit()
}
}

//if their are errors display them
if(isset($error)){
foreach($error as $error){
    echo "<p style='color:#ff0000'>$error</p>";
}
}
?> 

 <form action='' method='post'>
 <p><label>No</label><br><input type='text' name='no' value=''></p> 
 <p><label>Rating</label><br><input type='text' name='rating' value=''></p> 
 <p><label>Employee</label><br><input type='text' name='employee' value=''>     </p> 
 <p><label>Department</label><br><input type='text' name='department' value=''></p> 
 <p><label>Rate</label><br><input type='text' name='rate' value=''></p> 
 <p><label>County</label><br><input type='text' name='county' value=''></p> 
 <p><label>Number</label><br><input type='text' name='number' value=''></p>
 <p><label>Email</label><br><input type='text' name='email' value=''></p> 
 <p><label>Focalpoint Ref</label><br><input type='text' name='focalpointref' value=''></p> 
 <p><label>Confirmed On</label><br><input type='text' name='confirmedon' value=''></p> 
 <p><label>Comments</label><br><input type='text' name='comments' value=''></p> 
 <p><input type='submit' name='submit' value='Submit'></p> 
 </form>

0 个答案:

没有答案