php - header 重定向代码错误

标签 php mysql http-post post-redirect-get

我有包含在每个页面中的 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/

相关文章:

php - 是否有任何安全的方法可以从 bash 脚本作为 www-data 运行 CordovaCLI 命令?

php - 仅当数据存在于另一个 PHP 中时如何在表中插入数据

mysql按带点的数字排序

android - android TextView Mysql

ruby-on-rails - Rails:为第3方POST请求禁用CSRF

c# - 如何在 WinForm 中打开默认 Windows 浏览器并将 HTTP Post 数据发送到浏览器中打开的 Url?

java - 将动态值作为请求正文发布

PHP分页无序列表项

php - 对可能不包含协议(protocol)的字符串运行 parse_url()

php - 从 .php?id= 获取 MYSQL 查询