PHP评论框

时间:2013-08-12 13:40:15

标签: php html mysql mysqli comments

我的网站仍在建设中。

我在PHP中有一个注释框,它链接到我的godaddy帐户上的mysql数据库。

在调试过程中,我发现在您发表评论后刷新评论会因某种原因再次出现。

本网站的链接为:www.borgcarr.com

有两个PHP代码;一个在开头,一个在我要求它显示评论。

HTML CODE:

<?php
  mysql_connect("localhost","root","root");
  mysql_select_db("Comment_Box");
  $name=$_POST['name'];
  $comment=$_POST['comment'];
  $submit=$_POST['submit'];

  $dbLink = mysql_connect("localhost", "root", "root");
      mysql_query("SET character_set_client=utf8", $dbLink);
      mysql_query("SET character_set_connection=utf8", $dbLink);

  if($submit)
  {
  if($name&&$comment)
  {
  $insert=mysql_query("INSERT INTO Comment_Table (name,comment) VALUES ('$name','$comment') ");
  }
  else
  {
  echo "<script>alert('Please fill out all fields.');</script>";/*echo "please fill out all fields";*/
  }
  }
  ?>


<html>
<head><title>Law Website</title>

  <link href='css/style.css' type='text/css' rel='stylesheet' /> 
    <link href="scripts/6/js-image-slider.css" rel="stylesheet" type="text/css" />

    <script src="scripts/6/js-image-slider.js" type="text/javascript"></script>
</head>
<body>
  <center>
  <div id="sitewrapper">
    <div id="header">
      <div id="header_content_left">
        Maltese Law
  </div>
</div>
<div id="menu">
  <div id="menu_left">
    <div id="sliderFrame">
      <div id="slider">
          <img src="images/court1.jpg" alt="" />
          <img src="images/court2.jpg" alt="" />
          <img src="images/court3.jpg" alt="" />
          <img src="images/court4.jpg" alt="" />
          <img src="images/court5.jpg" alt="" />
          <img src="images/court6.jpg" alt="" />
      </div>
    </div>
  </div>

  <div id="menu_right">
    <div id="menu_right_top">
      <ul id="menulist">
        <li><a href src="#"><img src="images/buttons/articles_btn.png" onmouseover="this.src='images/buttons/articles_btn_hover.png'" onmouseout="this.src='images/buttons/articles_btn.png'" alt="Articles"/></a></li>
        <li><a href src="#"><img src="images/buttons/interviews_btn.png" onmouseover="this.src='images/buttons/interviews_btn_hover.png'" onmouseout="this.src='images/buttons/interviews_btn.png'" alt="Articles"/></a></li>
        <li><a href src="#"><img src="images/buttons/events_btn.png" onmouseover="this.src='images/buttons/events_btn_hover.png'" onmouseout="this.src='images/buttons/events_btn.png'" alt="Events" /></a></li>
        <li><a href src="#"><img src="images/buttons/team_btn.png" onmouseover="this.src='images/buttons/team_btn_hover.png'" onmouseout="this.src='images/buttons/team_btn.png'" alt="The Team" /></a></li>
        <li><a href src="#"><img src="images/buttons/contact_btn.png" onmouseover="this.src='images/buttons/contact_btn_hover.png'" onmouseout="this.src='images/buttons/contact_btn.png'" alt="Contact Us" /></a></li>
        <li><a href src="#"><img src="images/buttons/home_btn.png" onmouseover="this.src='images/buttons/home_btn_hover.png'" onmouseout="this.src='images/buttons/home_btn.png'" alt="Home" /></a></li>
      </ul>    
    </div>
    <div id="menu_right_bottom"><hr width="560px"></div>
  </div>
</div>
<div id="content">
  <div id="content_left">
    <p>
      <h2>News</h2>
      Lorem Ipsum is simply dummy
      text of the printing and
      typesetting industry. Lorem
      Ipsum has been the industry's s
      tandard dummy text ever since the
      1500s, when an unknown printer too
      k a galley of type and scrambled i
      t to make a type specimen book. It
      has survived not only five centuri
      es, but also the leap into electro
      nic typesetting, remaining essenti
      ally unchanged. It was popularised
      in the 1960s with the release of L
      etraset sheets containing Lorem Ip
      sum passages, and more recently wi
      th desktop publishing software lik
      e Aldus PageMaker including versio
      ns of Lorem Ipsum.
    </p>
  </div>
  <div class="verticalLine">
     <font style="color: #2a3035;">.</font>
  </div>
  <div id="content_right_top"><h1>Have your say...</h1><hr>
    <?php
      $dbLink = mysql_connect("localhost", "root", "root");
          mysql_query("SET character_set_results=utf8", $dbLink);
          mb_language('uni');
          mb_internal_encoding('UTF-8');

      $getquery=mysql_query("SELECT * FROM Comment_Table ORDER BY id DESC");
      while($rows=mysql_fetch_assoc($getquery))
      {
      $id=$rows['id'];
      $name=$rows['name'];
      $comment=$rows['comment'];
      echo $name . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' . '<hr size="1"/>'
      ;}
      ?>
       </div>
       <div id="content_right">

         <form action="index.php" method="POST">
         <table>
        <tr><td>Name: <br><input type="text" name="name"/></td></tr>
        <tr><td colspan="2">Comment: </td></tr>
         <tr><td colspan="5"><textarea name="comment" rows="5" cols="50"></textarea></td>  </tr>
        <tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
        </table>
        </form>

      </div>
    </div>
     <div id="footer">
      <div id="footer_content">
      Copyright &copy Maltese Law
        </div>
     </div>
  </div>
  </center>
</body>
 </html>

2 个答案:

答案 0 :(得分:1)

重定向防止了很多,在这种情况下:数据库内存浪费&amp;有些沮丧

答案是,当您刷新页面时,浏览器会将信息重新发送到服务器,然后再次添加信息,从而创建副本。

所以你必须做的是你应该有一个机制,在提交数据后将用户重定向到页面..

像这样:

header('Location: http://www.example.com/');

只需将http://www.example.com替换为您的网页,但请确保它不是www.borgcarr.com/index.php,因为它会导致无限重定向循环..

所以你需要做的是将其重定向到其他页面,并取消设置帖子变量...... 在另一页上你可能有一个按钮,上面写着«回到家或者其他......

所以整个过程将是:

unset($_POST);
header('Location: http://www.example.com/');

希望有所帮助,所以只需留下评论告诉它是否有效......

然而,这里最好的选择是有一个单独的页面来添加评论并将它们重定向到显示评论的主页面......

答案 1 :(得分:-1)

正如我在下面的评论中所说..我对此有类似的设置,

您可以添加以下内容:

if (isset($_POST['submit'])){
            $Errors = 0;
        if (empty($_POST['name'])){
            $Errors++;
        }
        if (empty($_POST['comment'])){
            $Errors++;
        }

        if ($Errors !== 0){
            echo "<script>alert('Please fill out all fields.');</script>";
        }else{
            $insert=mysql_query("INSERT INTO Comment_Table (name,comment) VALUES ('".mysql_real_escape_string($_POST['name'])."','".mysql_real_escape_string($_POST['comment'])."') ");
                unset($_POST);
            if (!headers_sent()){
                header("Location: index.php");
            }else{
                echo '<meta http-equiv="refresh" content="1; url=index.php">';
            }

        }

    }