我试图以insert.php作为操作,从表单中发布数据。但是,一旦数据发布到数据库,我就不能重定向回index.php。
为了找到答案,我搜索了以下网站:
以及关于这个问题的十个堆叠问题。
下面是insert.php文件的代码:
<?php
include 'connect.php';
$id = $_POST['id'];
$idea = mysql_real_escape_string($_POST['new_idea']);
if(!$_POST['submit']) {
echo "Please fill out the form!";
header('Location: index.php');
} else {
mysql_query("INSERT INTO user_idea (`id`, `idea`, `time_created`) VALUES(NULL, '$idea', NULL)") or die(mysql_error());
echo "Idea has been added!";
header('Location: index.php');
}?> 根据我收集的信息,如果前面有文本输出,则header()函数将不会执行。我已经尝试了这个功能,没有回显“想法被添加了!”和回声“请填写表格!”,但我仍然没有得到一个重定向。
提前谢谢你的建议。
-MF
发布于 2012-11-29 05:57:36
围绕方法工作:在页面顶部使用ob_start()
其他方法:在页面中启动<?php或?>之后,请省略任何空白,并在header()之后使用exit()
发布于 2012-11-29 05:58:43
发布于 2012-11-29 06:00:28
尝尝这个
<?php
include 'connect.php';
$id = $_POST['id'];
$idea = mysql_real_escape_string($_POST['new_idea']);
if(!$_POST['submit']) {
$message = "Please fill out the form!";
header('Location: index.php?message='.$message); exit;
} else {
mysql_query("INSERT INTO user_idea (`id`, `idea`, `time_created`) VALUES(NULL, '$idea', NULL)") or die(mysql_error());
$message = "Idea has been added!";
header('Location: index.php?message='.$message); exit;
}?> 将错误消息传递给index.php并显示在那里。
https://stackoverflow.com/questions/13619715
复制相似问题