header位置:函数在我的代码中不起作用

时间:2017-01-15 05:39:27

标签: php mysql

我有一个包含基本信息(Fname, Lname, Uname, Email, Phumber, Age, Facebook, Major, Education, Music, Fav, message

的网络表单

提交表单并将这些字段的数据插入MySQL数据库后,我希望能够重定向到不同的URL。
我尝试使用PHP的头函数,但由于某种原因,它不会将我重定向到我指定的URL。

    <?php
ob_start();
require("config.php");
include 'config.php';
ini_set('display_errors', 0);
if($_GET['action'] == 'check')
{
   if($_POST['Fname'] == '' || $_POST['Lname'] == '' || $_POST['Uname'] == '' || $_POST['Email'] == '' || $_POST['Phumber'] == '' || $_POST['Age'] == '' || $_POST['Facebook'] == '' || $_POST['Major'] == '' || $_POST['Education'] == '' || $_POST['Music'] == '' || $_POST['Fav'] == '' || $_POST['message'] == '')
   {
      exit;
   }
   else
   {
if($_POST['Email'] !== $_POST['Email'])
         {
         exit;
         }
      else
         {
         $result = mysql_query("SELECT * FROM loginphp
         WHERE Uname='{$_POST['Uname']}'") or die(mysql_error()); 
         $row = mysql_fetch_array( $result );
         if($row['Uname'] == '')
            {
            $_Fname = str_replace("<", "", $_POST['Fname']);
            $_Lname = str_replace("<", "", $_POST['Lname']);
            $_Uname = str_replace("<", "", $_POST['Uname']);
            $_Email = str_replace("<", "", $_POST['Email']);
$_Phumber = str_replace("<", "", $_POST['Phumber']);
$_Age = str_replace("<", "", $_POST['Age']);
$_Facebook = str_replace("<", "", $_POST['Facebook']);
$_Major = str_replace("<", "", $_POST['Major']);
$_Education = str_replace("<", "", $_POST['Education']);
$_Music = str_replace("<", "", $_POST['Music']);
$_Fav = str_replace("<", "", $_POST['Fav']);
$_message = str_replace("<", "", $_POST['message']);
            mysql_query("INSERT INTO loginphp 
            (Fname, Lname, Uname, Email, Phumber, Age, Facebook, Major, Education, Music, Fav, message) VALUES('$_Fname', '$_Lname', '$_Uname', '$_Email', '$_Phumber', '$_Age', '$_Facebook', '$_Major', '$_Education', '$_Music', '$_Fav', '$_message') ") 
            or die(mysql_error());
header("Location: main.php");
exit();
    }
}
}   
}
?>
  

警告:无法修改标头信息 - 第47行register.php中已发送的标头(在register.php:1处开始输出)

--------------答案

这个问题并不想解决,所以我添加了带有html代码的print,在插入数据后重定向页面

      mysql_query("INSERT INTO loginphp 
            (Fname, Lname, Uname, Email, Phumber, Age, Facebook, Major, Education, Music, Fav, message) VALUES('$_Fname', '$_Lname', '$_Uname', '$_Email', '$_Phumber', '$_Age', '$_Facebook', '$_Major', '$_Education', '$_Music', '$_Fav', '$_message') ") 
            or die(mysql_error());
echo '<META HTTP-EQUIV="Refresh" CONTENT="0;URL=http://www.http://stackoverflow.com/">';
exit();

感谢大家的帮助!

2 个答案:

答案 0 :(得分:0)

删除标题之前的所有输出,例如echoprint

并在标题后使用exit。

 header("Location: main.php");
 exit();

更新

如果打开输出缓冲,则可以在脚本中输出,但PHP不必发送标头,直到刷新缓冲区。 ...在文件中使用echo,print()或printr()检查一些内容。可能是这是问题或者如果有任何文件,那么检查空格的数量。

  $form = "<br>";
  //show the form
  $form .="<center>";
  $form .= '<form method .....';

...... ......

?>

之前的最后一行写下来
   echo $form;

答案 1 :(得分:0)

在更改标头之前,您必须删除所有echoprint_rvar_dump功能。在您的情况下,如果您在echo文件中没有config.php个类似功能,则可以试试这个:

//ini_set('display_errors', 1); commented in editing
ob_start();
//include the header
require("config.php");
if($_GET['action'] == 'check'){
//your code
}
//other codes (echo and print and vardump)

最好使用Twig等模板引擎