I have a website which is still under construction.
I have a comment box in PHP which is linked to a mysql database on my godaddy account.
While debugging I discovered that after you leave a comment and you refresh the comment shows up again for some reason.
The link for this website is: www.borgcarr.com
There are two php codes; one in the beginning and one where I require it to display the comments.
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 © Maltese Law
</div>
</div>
</div>
</center>
</body>
</html>
Redirection prevents a lot, in this case: Database memory wastage & some frustration
The answer is that when you refresh the page, the browser resends the information to the server which then adds the information again thereby creating a duplicate.
So what you must do is you should have a mechanism to redirect the user to the page after submitting the data..
like so:
header('Location: http://www.example.com/');
just replace http://www.example.com
with your page but make sure it is not www.borgcarr.com/index.php
because it will result in an infinite redirect loop..
so what you have to do is redirect it to some other page, and unset the post variables... on that other page you might have a button saying « Back to home or whatever...
so the whole process would be:
unset($_POST);
header('Location: http://www.example.com/');
hope that helps, so just leave a comment telling if it worked...
however, the best bet here is to have a separate page to add comments and redirect them to the main page which displays the comments...