使用 php 创建将用户输入写入文本文件的表单
Using php to create a form that writes user input to a text file
我是 php 的新手(3.5 周),我正在为我正在学习的 php 课程做一个项目。我已经处理这个 php 文件好几天了,我正处于崩溃的边缘。这是我想要实现的目标:
"Create a document with a form that registers bowlers for a bowling tournament. Use a single text file that saves information for each bowler on a separate line. Include the bowler’s name, age, and aver- age, separated by commas. Ensure that the Projects directory has read and write permissions for everyone."
我已经成功创建了表单,而且我认为我的变量设置正确。我想我需要使用一个数组来存储基于用户输入的每个变量。我试过写这个,但我打破了一切。到目前为止,这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>List of Bowlers</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1 style="text-transform: uppercase; color: #666666; font-weight: 400; font-size: 1.5em; letter-spacing: 5px;">Register for the upcoming bowling tournament</h1>
<?php
if (isset($_POST['first_name']) && isset($_POST['last_
name'])) {
$BowlerFirstName = addslashes($_POST['first_name']);
$BowlerLastName = addslashes($_POST['last_name']);
$BowlerAge = addslashes($_POST['bowler_age']);
$BowlerAverage = addslashes($_POST['bowler_average']);
$NewBowler = "$BowlerLastName, $BowlerFirstName, $BowlerAge, $BowlerAverage\r\n";
$BowlersFile = "bowlers.txt";
if (file_put_contents($BowlersFile, $NewBowler,
FILE_APPEND) > 0)
echo "<p>" . stripslashes($_POST['first_name']) .
" " . stripslashes($_POST['last_name']) .
" has been registered for the upcoming bowling tournament.</p>\n";
else
echo "<p>An error has occurred in your registration. Please try again.</p>";
} else
echo "<p>To successfully enter the upcoming bowling tournament, enter
your first and last name and click the Register
button.</p>";
?>
<form action="bowling_names.php" method="POST">
<p>First Name: <input type="text" name="first_name"
size="30" /></p>
<p>Last Name: <input type="text" name="last_name"
size="30" /></p>
<p>Age: <input type="text" name="bowler_age"
size="30" /></p>
<p>Average: <input type="text" name="bowler_average"
size="30" /></p>
<p><input type="submit" value="Register" /></p>
</form>
<?php
$BowlersList = readfile("bowlers.txt");
echo $BowlersList;
?>
</body>
</html>
我真的很想学习这里的概念。如果您愿意提供帮助,请也说明我哪里出错了。我非常感谢任何帮助!
这段代码有一个问题:
if (isset($_POST['first_name']) && isset($_POST['last_
name'])) {
你的 "last_name" 的损坏的 POST 数组将阻止它写入你的文件。
需要在一起:
if (isset($_POST['first_name']) && isset($_POST['last_name'])) {
然后,确保它的文本文件存在,否则你会收到如下警告:(如果你的系统设置为 catch/display 错误、警告、通知)
Warning: readfile(bowlers.txt): failed to open stream: No such file or directory...
还要确保它具有适当的写入权限(如果它已经存在)。
通常644就够了。作为最后的手段,您可以使用 777,但这不是一个安全的设置。
文件夹本身也需要适当的写入权限。 755 是推荐设置。
将 error reporting 添加到您的文件的顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:显示错误只应在试运行中进行,绝不能在生产中进行。
此外,您的文件内容现在将在一行中回显。
如果您希望它们在不同的行中,您可以使用以下内容:
$file = fopen("bowlers.txt", "r");
$i = 0;
while (!feof($file)) {
$lines[] = fgets($file);
}
fclose($file);
foreach (lines as $x){
echo $x.'<br/>';
}
有几点我可以指出:
1) 此行不能有换行符:
if (isset($_POST['first_name']) && isset($_POST['last_
name'])) {
不知道你的原代码有没有,还是只有贴在这里的。如果是,请将其删除,这样它会显示:
if (isset($_POST['first_name']) && isset($_POST['last_name'])) {
2) 函数 readfile()
outputs the file contents directly to the output buffer, it doesn't return its contents, it returns the number of bytes read. So, to read the entire file to a variable you would use file_put_contents()
代替。
3) 我想您希望 $BowlersList
成为一个数组。为此,您需要逐行读取文件,然后使用 explode()
将每一行拆分为一个数组。像这样:
$fileName = "bowlers.txt";
$BowlersList = array();
if (file_exists($fileName)) {
$file = fopen($fileName, "r");
while(!feof($file)){
$line = fgets($file);
if ($line != "") {
$BowlersList[] = explode(", ", $line);
}
}
fclose($file);
} else {
echo "Bowlers file doesn't exist.";
}
echo "<pre>";print_r($BowlersList);echo "</pre>";
我是 php 的新手(3.5 周),我正在为我正在学习的 php 课程做一个项目。我已经处理这个 php 文件好几天了,我正处于崩溃的边缘。这是我想要实现的目标:
"Create a document with a form that registers bowlers for a bowling tournament. Use a single text file that saves information for each bowler on a separate line. Include the bowler’s name, age, and aver- age, separated by commas. Ensure that the Projects directory has read and write permissions for everyone."
我已经成功创建了表单,而且我认为我的变量设置正确。我想我需要使用一个数组来存储基于用户输入的每个变量。我试过写这个,但我打破了一切。到目前为止,这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>List of Bowlers</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1 style="text-transform: uppercase; color: #666666; font-weight: 400; font-size: 1.5em; letter-spacing: 5px;">Register for the upcoming bowling tournament</h1>
<?php
if (isset($_POST['first_name']) && isset($_POST['last_
name'])) {
$BowlerFirstName = addslashes($_POST['first_name']);
$BowlerLastName = addslashes($_POST['last_name']);
$BowlerAge = addslashes($_POST['bowler_age']);
$BowlerAverage = addslashes($_POST['bowler_average']);
$NewBowler = "$BowlerLastName, $BowlerFirstName, $BowlerAge, $BowlerAverage\r\n";
$BowlersFile = "bowlers.txt";
if (file_put_contents($BowlersFile, $NewBowler,
FILE_APPEND) > 0)
echo "<p>" . stripslashes($_POST['first_name']) .
" " . stripslashes($_POST['last_name']) .
" has been registered for the upcoming bowling tournament.</p>\n";
else
echo "<p>An error has occurred in your registration. Please try again.</p>";
} else
echo "<p>To successfully enter the upcoming bowling tournament, enter
your first and last name and click the Register
button.</p>";
?>
<form action="bowling_names.php" method="POST">
<p>First Name: <input type="text" name="first_name"
size="30" /></p>
<p>Last Name: <input type="text" name="last_name"
size="30" /></p>
<p>Age: <input type="text" name="bowler_age"
size="30" /></p>
<p>Average: <input type="text" name="bowler_average"
size="30" /></p>
<p><input type="submit" value="Register" /></p>
</form>
<?php
$BowlersList = readfile("bowlers.txt");
echo $BowlersList;
?>
</body>
</html>
我真的很想学习这里的概念。如果您愿意提供帮助,请也说明我哪里出错了。我非常感谢任何帮助!
这段代码有一个问题:
if (isset($_POST['first_name']) && isset($_POST['last_
name'])) {
你的 "last_name" 的损坏的 POST 数组将阻止它写入你的文件。
需要在一起:
if (isset($_POST['first_name']) && isset($_POST['last_name'])) {
然后,确保它的文本文件存在,否则你会收到如下警告:(如果你的系统设置为 catch/display 错误、警告、通知)
Warning: readfile(bowlers.txt): failed to open stream: No such file or directory...
还要确保它具有适当的写入权限(如果它已经存在)。
通常644就够了。作为最后的手段,您可以使用 777,但这不是一个安全的设置。
文件夹本身也需要适当的写入权限。 755 是推荐设置。
将 error reporting 添加到您的文件的顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:显示错误只应在试运行中进行,绝不能在生产中进行。
此外,您的文件内容现在将在一行中回显。
如果您希望它们在不同的行中,您可以使用以下内容:
$file = fopen("bowlers.txt", "r");
$i = 0;
while (!feof($file)) {
$lines[] = fgets($file);
}
fclose($file);
foreach (lines as $x){
echo $x.'<br/>';
}
有几点我可以指出:
1) 此行不能有换行符:
if (isset($_POST['first_name']) && isset($_POST['last_
name'])) {
不知道你的原代码有没有,还是只有贴在这里的。如果是,请将其删除,这样它会显示:
if (isset($_POST['first_name']) && isset($_POST['last_name'])) {
2) 函数 readfile()
outputs the file contents directly to the output buffer, it doesn't return its contents, it returns the number of bytes read. So, to read the entire file to a variable you would use file_put_contents()
代替。
3) 我想您希望 $BowlersList
成为一个数组。为此,您需要逐行读取文件,然后使用 explode()
将每一行拆分为一个数组。像这样:
$fileName = "bowlers.txt";
$BowlersList = array();
if (file_exists($fileName)) {
$file = fopen($fileName, "r");
while(!feof($file)){
$line = fgets($file);
if ($line != "") {
$BowlersList[] = explode(", ", $line);
}
}
fclose($file);
} else {
echo "Bowlers file doesn't exist.";
}
echo "<pre>";print_r($BowlersList);echo "</pre>";