-1

我有一个仍在建设中的网站。

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

在调试时,我发现在您发表评论并刷新评论后,由于某种原因再次出现。

该网站的链接是:www.borgcarr.com

有两个php代码;一个在开头,一个在我需要它显示评论的地方。

HTML 代码:

<?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>
4

2 回答 2

1

在这种情况下,重定向可以防止很多事情:数据库内存浪费和一些挫败感

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

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

像这样:

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

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

所以你要做的就是将它重定向到其他页面,并取消设置帖子变量......在另一个页面上,你可能有一个按钮说“返回主页或其他......

所以整个过程是:

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

希望对您有所帮助,所以只需发表评论,告诉它是否有效......

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

于 2013-08-12T13:46:52.033 回答
-1

As Said in my comment below.. I have a similar setup to this,

You could add the following:

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">';
            }

        }

    }
于 2013-08-12T14:11:30.333 回答