gpt4 book ai didi

php - 查询成功后跳转到首页

转载 作者:太空宇宙 更新时间:2023-11-03 11:56:16 24 4
gpt4 key购买 nike

<分区>

我有一个简单的表单可以将用户插入到我的数据库中,但我希望表单在查询完成后重定向到我的主页。我尝试了几种方法,要么出现错误,要么查询未运行,要么不重定向。这是我最后一次试用的表格页面

<?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';"。有什么建议吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com