留言簿PHP脚本建议
我正在用php制作一个简单的留言簿脚本。我基本上已经完成了,但需要帮助查找代码中的逻辑错误。一个是,当从表单内容回显结果时。我想显示电子邮件,全名和评论,每一行 像这样留言簿PHP脚本建议,php,html,Php,Html,我正在用php制作一个简单的留言簿脚本。我基本上已经完成了,但需要帮助查找代码中的逻辑错误。一个是,当从表单内容回显结果时。我想显示电子邮件,全名和评论,每一行 像这样 email: some@email.com Name: joe somebody Comment: hello world. 但出于某种原因,它只是在所有行上显示名字和姓氏 其他3个问题是排序(升序、降序)和删除重复项 提前感谢您对如何解决此问题的建议 我只是要求你说得详细一点,这样我就知道你在说什么了 在任何情况下,代码如
email: some@email.com
Name: joe somebody
Comment:
hello world.
但出于某种原因,它只是在所有行上显示名字和姓氏
其他3个问题是排序(升序、降序)和删除重复项
提前感谢您对如何解决此问题的建议
我只是要求你说得详细一点,这样我就知道你在说什么了
在任何情况下,代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Guest Book</title>
</head>
<body>
<?php
// is there data in the fields
if (isset($_POST['submit'])){
$firstName = addslashes($_POST['first_name']);
$lastName = addslashes($_POST['last_name']);
$wholeName = $firstName . "," . $lastName;
$email = addslashes($_POST['email']);
$comment = addslashes($_POST['comment']);
$guestRecord = "$wholeName~$email~$comment\n";
$guestBook = fopen("guestbook.txt", "ab");
//if not let the user know
if ($guestBook === FALSE)
echo "There was an error saving your entry!\n";
else {
// if there is info add it to the txt file
fwrite($guestBook, $guestRecord);
// close the txt file
fclose($guestBook);
//let the user know info was saved
echo "Your entry has been saved.\n";
}
}
?>
<h2>Enter your name to sign our guest book</h2>
<hr>
<form method="POST" action ="GuestBook1.php">
<p>First Name<input type ="text" name="first_name"/></p>
<p>Last Name<input type ="text" name="last_name"/></p>
<p>Email<input type ="text" name="email"/></p>
<p><textarea name="comment" id="comment"/></textarea></p>
<p><input type ="submit" value ="Submit" name="submit"/></p>
</form>
<hr>
<p><a href ="GuestBook.php">Show Guest Book</a>
<br />
<a href="GuestBook.php?action=Delete%20First">Delete Guest Entry</a>
<br />
<a href="GuestBook.php?action=Remove%20Duplicates">Remove Duplicate Entries</a>
<br />
<a href="GuestBook.php?action=Sort%20Ascending">Sort Entries A-Z</a>
<br />
<a href="GuestBook.php?action=Sort%20Descending">Sort Entries Z-A</a>
<br />
</p>
<?php
// if theres info in the form process info
if (isset($_GET['submit'])) {
if ((file_exists("guestbook.txt")) &&
(filesize("guestbook.txt") != 0)) {
$guestArray = file("guestbook.txt");
//switch to $_Get Method
switch ($_GET['submit']) {
// remove duplicate entries
case 'Remove Duplicates':
$guestArray = array_unique($guestRecord);
$guestArray = array_values($guestRecord);
break;
//sort ascending
case 'Sort Ascending':
$guestArray = sort($guestArray);
break;
//sort Decending
case 'Sort Decending':
$guestArray = ksort($guestArray);
break;
}
//count to see how many entries there are
if (count($guestArray)>0) {
$newGuest = implode($guestArray);
$guestStore = fopen("guestbook.txt", "ab");
if ($guestStore === false)
echo "There was an error updating the message file\n";
else {fwrite($guestStore, $newGuest);
fclose($guestStore);
}
}
else
unlink("guestbook.txt");}
}
if ((!file_exists("guestbook.txt")) ||
(filesize("guestbook.txt") == 0))
echo "<p>There are no entries.</p>\n";
else {
// there isnt anything in the txt file show an error
if ((!file_exists("guestbook.txt")) ||
(filesize("guestbook.txt") == 0))
echo "<p>There are no entries.</p>\n"; else {
//if there is information display the txt file in a table
$guestArray = file("guestbook.txt");
echo "<table style=\"background-color:lightgray\"
border=\"1\" width=\"100%\">\n";
//begin counting number of guest entries
$count = count($guestArray);
for ($i = 0; $i < $count; ++$i) {
$currMsg = explode("~", $guestArray[$i]);
// display results in a table
echo "<td width=\"5%\" style=\"text-align:center;
font-weight:bold\">" . ($i + 1) . "</td>\n";
echo "<td width=\"95%\"><span style=\"font-weight:bold\">
Email:</span> " . htmlentities($currMsg[0]) . "<br />\n";
echo "<span style=\"font-weight:bold\">Name:</span> " .
htmlentities($currMsg[0]) . "<br />\n";
echo "<span style=\"text-decoration:underline;
font-weight:bold\">Comment</span><br />\n" .
htmlentities($currMsg[0]) .
"</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
}
}
?>
</body>
</html>
留言簿
输入您的姓名以在我们的宾客登记簿上签名
名字
姓氏
电子邮件
关于您的第一期,您正在为所有字段回显$currMsg[0]
。您需要为每个值使用正确的索引(0
表示名称,1
表示电子邮件,2
表示注释)。或者更好的方法是,使用以下内容:
list($name, $email, $comment) = explode("~", $guestArray[$i]);
这样您就有了有意义的变量名
其他问题需要更具体地说明您正在尝试做什么(以及当前代码以何种方式无法工作)
但我至少看到了一个问题-您的链接有?action=
,但您的开关
语句关注的是$\u GET['submit']
,而不是$\u GET['action']
。另外,您在案例“排序降级”中有一个输入错误:
(缺少“s”)
\r\n\
将物理上把东西放在新行上。请确保使用双引号。当您谈到排序时,您希望按什么排序?姓名(姓还是姓)?电子邮件议论还有别的吗?一个db比一个平面文件容易一百万倍
$firstName = addslashes($_POST['first_name'])."\r\n";