我目前正在为大学做一个项目,但我遇到了问题。我有两页,每页都有一个表单,其中包含三个文本字段(des,act,date),我正在尝试这样做,以便它将表单中的信息添加到文本文档中,但目前它所做的只是覆盖它。有人知道如何解决这个问题吗?
第 1 页
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
//Send Data
$content = 'OBSERVATION'."'r'n".'Breif Description: '.$_POST['des1']."'r'n".'Agreed Action: '.$_POST['act1']."'r'n".'Close Date: '.$_POST['date1']."'r'n";
if (isset($_POST['submit'])){
$myFile=fopen("Observation.txt","w") or exit("Can’t open file!");
fwrite($myFile, $content);
fclose($myFile);
header( 'Location: http://www.murphy.sulmaxmarketing.com/GoodPractices.php' ) ;
}
?>
第 2 页
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
//Send Data
$content = "'r'n'r'n".'GOOD PRACTICES'."'r'n".'Breif Description: '.$_POST['des2']."'r'n".'Agreed Action: '.$_POST['act2']."'r'n".'Close Date: '.$_POST['date2']."'r'n";
if (isset($_POST['submit'])){
$myFile=fopen("Observation.txt","w") or exit("Can’t open file!");
fwrite($myFile, $content);
fclose($myFile);
}
?>
fopen() 具有 'w'
模式
仅开放供书写;将文件指针放在文件的开头,并将文件截断为零长度。如果该文件不存在,请尝试创建它。
具有'a'
模式的 fopen()
打开仅供写入;将文件指针放在文件的末尾。如果该文件不存在,请尝试创建它。在这种模式下,fseek() 不起作用,总是附加写入。
将file_put_contents
函数与FILE_APPEND
标志一起使用。
此函数与调用 fopen()、fwrite() 和 fclose() 相同。 依次将数据写入文件。
FILE_APPEND :如果文件名已存在,请将数据附加到文件中,而不是覆盖它。
...
if (isset($_POST['submit'])) {
file_put_contents("Observation.txt", $content, FILE_APPEND);
header( 'Location: http://www.murphy.sulmaxmarketing.com/GoodPractices.php' ) ;
exit;
}
...
http://php.net/manual/en/function.file-put-contents.php
使用 file_put_content
if (isset($_POST['submit'])) {
file_put_contents("Observation.txt", $content, FILE_APPEND);
... your code here
}
在这里,"FILE_APPEND"中的第三个参数file_put_content每次在以前的代码中使用新内容时都会附加您的文件,由于名称相同,它被覆盖了一个内容,因此如果您想以这种方式执行此操作,则要设置两个文件的不同名称。
这里file_put_content函数网址:http://php.net/manual/en/function.file-put-contents.php