我正在获取表单数据并将其写入服务器上的csv文件。我一直遇到同样类型的问题,我不知道我做错了什么。列标题与每个条目重复,或者每个附加提交都在同一行上写入。另外,我如何获取$ _POST数据并填充csv?我必须声明它两次才能使这个工作到此为止。这就是我的工作方式。
<?php
//this is just for the post data, but I'm having to declare the key/values again below. I'm sure this is wrong.
$email = $_POST['email'];
//rest of the form inputs go here same as $email
//some server side validation builds an 'error' array
if(!isset($error) {
$del = ", ";
//this is where I have to repeat the post data key/values
$data[1] = $_POST['email'];
$data[2] = $_POST['FirstName'];
$data[3] = $_POST['LastName'];
$data[4] = $_POST['Address'];
$data[5] = $_POST['City'];
$data[6] = $_POST['Country'];
$data[7] = $_POST['State'];
$data[8] = $_POST['Phone1'];
$data[9] = $_POST['password'];
$file = fopen("Registration.csv", "a");
//$data is the column headers that won't behave
$data = "Email, First Name, Last Name, Address, City, Country, State, Phone, Password
".implode($del, $data);
fwrite($file, $data);
fclose($file);
}
?>
答案 0 :(得分:2)
好的,请尝试以下方法:
此代码检查文件是否为空,如果是,则写入标题。
== Compilation error on file web/helpers/sender_helper.ex ==
** (ArgumentError) *gettext macros expect translation keys (msgid and msgid_plural) and
domains to expand to strings at compile-time, but the given msgid
doesn't.
Dynamic translations should be avoided as they limit gettext's
ability to extract translations from your source code. If you are
sure you need dynamic lookup, you can use the functions in the Gettext
module:
string = "hello world"
Gettext.gettext(MyApp.Gettext, string)
(gettext) lib/gettext/compiler.ex:196: anonymous fn/2 in Gettext.Compiler.expand_to_binary/4
expanding macro: MyApp.Gettext.dgettext_noop/2
web/helpers/push_helper.ex:23: MyApp.SenderHelper.translated_msg/2
expanding macro: MyApp.Gettext.dgettext/3
web/helpers/push_helper.ex:23: MyApp.SenderHelper.translated_msg/2
expanding macro: MyApp.Gettext.gettext/1
web/helpers/push_helper.ex:23: MyApp.SenderHelper.translated_msg/2
(elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
$headers = "Email, First Name, Last Name, Address, City, Country, State, Phone, Password";
if (0 == filesize( $file_path )) {
fwrite($file, $headers);
}
&#13;
答案 1 :(得分:0)
$myfile = fopen("Registration.csv", "a") or die("Unable to open file!");
$data = "Email, First Name, Last Name, Address, City, Country, State, Phone, Password
".implode($del, $data);
fwrite($myfile, "\n". $txt);
fclose($myfile);
我希望这有帮助