【发布时间】:2017-02-03 07:56:33
【问题描述】:
我正在尝试更新 txt 文件中的内容,以便每次按下寄存器 +1 时都会将一个添加到该特定数据中。到目前为止,它显示数据和注册按钮,但是当我单击它时,它不会添加一个。我该如何做到这一点?
<!DOCTYPE html>
<html>
<body>
<?php
$myfile = fopen("addcourse.txt", "a+") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . '<form id="h1" class="rounded" action="final.php" target="" method="post"/>
<input type="submit" name="submit" class="button" value="Register" /> </form>' "<br>";
}
fclose($myfile);
?>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
$data = $_POST['field1'];
$ret = file_put_contents('/addcourse.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
【问题讨论】:
-
你有什么错误吗?
-
无,因为此代码是正确的,但我每次单击提交时都尝试添加 1。它没有
-
请在
<?php之后添加error_reporting(E_ALL);ini_set('display_errors',1);并检查可能的错误 -
我打开了error_reporting,但没有任何错误,如果我必须在我正在获取的数据旁边添加“+1”字符串,你会说怎么做?
标签: php file file-handling