查询成功后跳转到首页
Redirect to homepage after successful query
我有一个简单的表单可以将用户插入我的数据库,但我希望表单在查询完成后重定向到我的主页。我尝试了几种方法,或者我收到错误,查询不是 运行 或者它没有重定向。这是我上次试用的表格页面
<?php
require("db.php");
?>
<?php include 'query.php';?>
<html>
<head>
<title>Insert a Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST['insert']))
{
$user_added = $_POST['user_added'];
$insert_query = "INSERT INTO userList ";
$insert_query .= "(Book, User_0, User_1) ";
$insert_query .= "VALUES ";
$insert_query .= "('$user_added', '0', '1')";
$retval = mysqli_query($connection, $insert_query);
if(!$retval )
{
die ("The error is: " . mysqli_error($connection));
}
echo "Updated data successfully\n";
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Book to Add</td>
<td><input name="user_added" type="text" id="user_added"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="insert" type="submit" id="insert" value="Insert" onclick="window.location='home.php';">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
<?php
mysqli_close($connection);
?>
当我以这种方式尝试时,它显示了 Updated data successfully
文本,但它没有重定向。我尝试采用以下方法
if(!$retval )
{
die ("The error is: " . mysqli_error($connection));
}
else
{
header("Location: test.php");
echo "Updated data successfully\n";
}
但我收到以下错误
Warning: Cannot modify header information - headers already sent by (output started at /home/myusername/public_html/home.php:11) in /home/myusername/public_html/home.php on line 30
Updated data successfully
因此数据库已更新,但重定向不起作用。当然,当我尝试第二种方法时,我从表单中删除了 onclick="window.location='home.php';"
。有什么建议吗?
在您的 header("Location: test.php");
之前添加以下内容,使其看起来像:
flush();
ob_flush();
header("Location: test.php");
您需要刷新 headers 才能重新设置它。
改变
header("Location: test.php");
到
echo "<html><script> window.location.href="test.php";</script></html>";
通过 PHP 的重定向受 headers 影响。 Headers,如错误所示,一旦页面输出开始就无法发送。
来自PHP manual:
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
在 PHP 是 运行 之前,您有 HTML。 (这通常不是一个好主意;理想情况下,数据库和其他准备工作 PHP 将在任何输出之前在单独的文件中完成,以便它们可以影响重定向和其他 headers。)
让您的代码正常工作的一种快速方法是回退到 JavaScript,它很乐意随时重定向。
if( !$retval)
die ("The error is: " . mysqli_error($connection));
else
echo "<script>location.href = 'somewhere.php';</script>";
你必须放置页眉("Location: test.php");在任何 echo 指令或 html 输出之前,所以在开始时进行测试
使用
<?php
ob_start();
?>
在页面顶部..希望对您有所帮助..
有关更多详细信息,请访问此 link
What's the use of ob_start() in php?
我有一个简单的表单可以将用户插入我的数据库,但我希望表单在查询完成后重定向到我的主页。我尝试了几种方法,或者我收到错误,查询不是 运行 或者它没有重定向。这是我上次试用的表格页面
<?php
require("db.php");
?>
<?php include 'query.php';?>
<html>
<head>
<title>Insert a Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST['insert']))
{
$user_added = $_POST['user_added'];
$insert_query = "INSERT INTO userList ";
$insert_query .= "(Book, User_0, User_1) ";
$insert_query .= "VALUES ";
$insert_query .= "('$user_added', '0', '1')";
$retval = mysqli_query($connection, $insert_query);
if(!$retval )
{
die ("The error is: " . mysqli_error($connection));
}
echo "Updated data successfully\n";
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Book to Add</td>
<td><input name="user_added" type="text" id="user_added"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="insert" type="submit" id="insert" value="Insert" onclick="window.location='home.php';">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
<?php
mysqli_close($connection);
?>
当我以这种方式尝试时,它显示了 Updated data successfully
文本,但它没有重定向。我尝试采用以下方法
if(!$retval )
{
die ("The error is: " . mysqli_error($connection));
}
else
{
header("Location: test.php");
echo "Updated data successfully\n";
}
但我收到以下错误
Warning: Cannot modify header information - headers already sent by (output started at /home/myusername/public_html/home.php:11) in /home/myusername/public_html/home.php on line 30
Updated data successfully
因此数据库已更新,但重定向不起作用。当然,当我尝试第二种方法时,我从表单中删除了 onclick="window.location='home.php';"
。有什么建议吗?
在您的 header("Location: test.php");
之前添加以下内容,使其看起来像:
flush();
ob_flush();
header("Location: test.php");
您需要刷新 headers 才能重新设置它。
改变
header("Location: test.php");
到
echo "<html><script> window.location.href="test.php";</script></html>";
通过 PHP 的重定向受 headers 影响。 Headers,如错误所示,一旦页面输出开始就无法发送。
来自PHP manual:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
在 PHP 是 运行 之前,您有 HTML。 (这通常不是一个好主意;理想情况下,数据库和其他准备工作 PHP 将在任何输出之前在单独的文件中完成,以便它们可以影响重定向和其他 headers。)
让您的代码正常工作的一种快速方法是回退到 JavaScript,它很乐意随时重定向。
if( !$retval)
die ("The error is: " . mysqli_error($connection));
else
echo "<script>location.href = 'somewhere.php';</script>";
你必须放置页眉("Location: test.php");在任何 echo 指令或 html 输出之前,所以在开始时进行测试
使用
<?php
ob_start();
?>
在页面顶部..希望对您有所帮助.. 有关更多详细信息,请访问此 link What's the use of ob_start() in php?