我有包含在每个页面中的 template.php 文件,所以现在我想向每个页面添加评论表单,我已将表单添加到 template.php,我遇到的唯一问题是刷新导致 DB 中的重复条目.所以经过一些研究我发现了 POST REDIRECT GET 方法,所以在 template.php 中我将表单操作放到 addcomment.php 中,在 addcomment.php 中我使用了以下代码:
<?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();
}
?>
我收到这个错误:
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
以及如何检查查询是否正常工作。
最佳答案
删除<?php
之前的这些空格它会起作用(假设你的包含文件中没有输出)
and also how to check if query worked properly or not.
您已经检查过了。查询失败会触发or die('error query');
关于php - header 重定向代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7774071/