Error in code for header redirect
I have template.php file which gets include in each page , so now I want to add comment form to each page , I 开发者_开发知识库have added the form to template.php , only problem I was having is refresh causes duplicate entry in DB .So after some research I found POST REDIRECT GET method , so in template.php I have put form action to addcomment.php and in addcomment.php I have used the following code:
<?php
require_once("appvars.php");
require_once("connectvars.php");
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc() === 1){
$_POST = array_map( 'stripslashes', $_POST );
$_GET = array_map( 'stripslashes', $_GET );
$_COOKIE = array_map( 'stripslashes', $_COOKIE );
}
if (isset($_POST['commentsubmit']))
{
$type=mysqli_real_escape_string($dbc,trim($_POST['type']));
$name=mysqli_real_escape_string($dbc,trim($_POST['username']));
$email=mysqli_real_escape_string($dbc,trim($_POST['useremail']));
$subject=mysqli_real_escape_string($dbc,trim($_POST['subject']));
$comment=mysqli_real_escape_string($dbc,trim($_POST['usercomment']));
$link=mysqli_real_escape_string($dbc,trim($_POST['link']));
$query = "INSERT INTO comments (`type`,`name`,`email`,`subject`,`comment`,`date`,`link`) VALUES ('$type','$name','$email','$subject','$comment',NOW(),'$link')";
$result=mysqli_query($dbc, $query) or die('error query');
header("Location: $link", 302);
exit();
}
?>
I am getting this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/animalsw/public_html/addcomment.php:1) in /home/animalsw/public_html/addcomment.php on line 22
and also how to check if query worked properly or not.
Remove these spaces before <?php
and it will work (assuming you have no output in your included files)
and also how to check if query worked properly or not.
You already check it. If query fails, it will trigger or die('error query');
To add to what genesis said, make sure require_once("appvars.php"); require_once("connectvars.php") do not echo out any data. Once you start outputting something, you will get that error because you will be sending headers when you're already outputting the body.
精彩评论