Reputation: 65
I have code for comments in php page
but when I am doing refresh page after posting comment then same comment is getting posted again and stored in database also. please help me
<?php
mysql_connect("localhost","root","password");
mysql_select_db("comments");
$name=$_POST['name'];
$comment=$_POST['comment'];
$submit=$_POST['submit'];
$dbLink = mysql_connect("localhost", "root", "a12345");
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 (name,comment) VALUES ('$name','$comment') ");
}
else
{
echo "please fill out all fields";
}}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comment box</title>
</head>
<body>
<center>
<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="10" cols="50"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
</table>
</form>
<?php
$dbLink = mysql_connect("localhost", "root", "a12345");
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
$getquery=mysql_query("SELECT * FROM comment 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"/>'
;
}
?>
</body>
</html>
I am using:
$name = isset($_POST['name']) ?
$_POST['name'] : '';
$comment = isset($_POST['comment']) ?
$_POST['comment'] : '';
$submit = isset($_POST['submit']) ?
$_POST['submit'] : '';
but without Benefit.
Upvotes: 0
Views: 512
Reputation: 2277
After submitting a comment, if you refresh the page, you are submitting the same comment over. That is why they are duplicated.
The best way to deal with this is to either put your INSERT query in another php file and then redirect to the comments page after submission or if you want to keep it in the same file, then redirect to the same page using header
function after you insert the comment to the database.
A simple way to fix:
if($submit)
{
if($name && $comment)
{
$insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') ");
header( 'Location: http://the url of your page') ;
}
else
{
echo "please fill out all fields";
}
}
Please note that your code is not secure.
Upvotes: 2
Reputation: 874
After doing your work with post you can unset POST
with unset($_POST)
.
This will empty the post and refresh will not bring values again.
Upvotes: -1
Reputation: 22721
You can add unset()
after the $insert
query
$insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') ");
unset($_POST['submit']);
header("location:yourfile.php");
Upvotes: 1
Reputation: 5309
Avoid this
$submit = isset($_POST['submit']) ?
$_POST['submit'] : '';
and use
if(isset($_POST['submit']))
{
//here the insertion query..
}
Upvotes: 0