GuestBook PHP脚本建议

时间:2014-03-06 03:00:46

标签: php html

我在php中制作了一个简单的留言板脚本。我几乎完成了它,但需要帮助在我的代码中找到逻辑错误。一个是在回应表单内容的结果时。我想在不同的行上显示电子邮件,全名和评论。

像这样

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>

2 个答案:

答案 0 :(得分:0)

$firstName = addslashes($_POST['first_name'])."\r\n";

\r\n\会将物理上的东西放在新的一行上。确保使用双引号。

答案 1 :(得分:-1)

关于您的第一个问题,您正在回复所有字段的$currMsg[0]。您需要为每个值使用正确的索引(名称为0,电子邮件为1,评论为2。甚至更好,使用类似的东西:

list($name, $email, $comment) = explode("~", $guestArray[$i]);

这样你就有了有意义的变量名。

您需要更具体地了解其他问题,以及您当前的代码无法正常工作的方式。

我可以看到至少一个问题 - 您的链接有?action=,但您的switch语句正在查看$_GET['submit'],而不是$_GET['action']。另外,您在case 'Sort Decending':(缺少's')中输入错字

相关问题