php - 插入数据后的PHP重定向
问题描述
我在插入数据后重定向到另一个页面时遇到问题。
我正在本地主机上工作,我试图在插入数据后重定向到另一个页面(url)。
使用下面的代码,我尝试将 google url 它不会重定向它在提交后保留在页面上。
任何帮助,将不胜感激。
谢谢
<?php
/**
* Template Name: Booking Template
*/
get_header();
require_once("db.php");
if(count($_POST)>0) {
$sql = "INSERT INTO bookings (reason, doctor, bookingDate, bookingTime, patientName, gender, phone, email) VALUES ('" . $_POST["reason"] . "','" . $_POST["doctor"] . "','" . $_POST["bookingDate"] . "','" . $_POST["bookingTime"] . "','" . $_POST["patientName"] . "','" . $_POST["gender"] . "','" . $_POST["phone"] . "','" . $_POST["email"] . "')";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
header("location: http://www.google.com");
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
mysqli_close($link);
?>
解决方案
如果您在header()
不会重定向之前回显或打印某些内容。所以你需要删除echo
之前的header()
. exit()
在header()
. _
/**
* Template Name: Booking Template
*/
get_header();
require_once("db.php");
if(count($_POST)>0) {
$sql = "INSERT INTO bookings (reason, doctor, bookingDate, bookingTime, patientName, gender, phone, email) VALUES ('" . $_POST["reason"] . "','" . $_POST["doctor"] . "','" . $_POST["bookingDate"] . "','" . $_POST["bookingTime"] . "','" . $_POST["patientName"] . "','" . $_POST["gender"] . "','" . $_POST["phone"] . "','" . $_POST["email"] . "')";
if(mysqli_query($link, $sql)){
//echo "Records inserted successfully."; remove or comment this line
header("location: http://www.google.com");
exit(); // use exit. It's a good practice
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
mysqli_close($link);
如果您的查询正常并且数据插入正确但尚未重定向,您可以使用Javascript
重定向。
/**
* Template Name: Booking Template
*/
get_header();
require_once("db.php");
if(count($_POST)>0) {
$sql = "INSERT INTO bookings (reason, doctor, bookingDate, bookingTime, patientName, gender, phone, email) VALUES ('" . $_POST["reason"] . "','" . $_POST["doctor"] . "','" . $_POST["bookingDate"] . "','" . $_POST["bookingTime"] . "','" . $_POST["patientName"] . "','" . $_POST["gender"] . "','" . $_POST["phone"] . "','" . $_POST["email"] . "')";
if(mysqli_query($link, $sql)){
//echo "Records inserted successfully."; remove or comment this line
header("location: http://www.google.com");
echo "<script>window.location = 'http://www.google.com';</script>";
exit(); // use exit. It's a good practice
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
mysqli_close($link);
确保函数没有错误,get_header()
并仔细检查代码中是否存在任何可能的错误。仅当您现在需要解决它时才使用第二种方法。
推荐阅读
- python - 我应该使用数据结构而不是使用原始方法吗?
- recursion - MIPS 递归函数来反转链表的顺序
- python - python - 如何对List中的元素求和并在Python中总和为n时使用它们创建子列表
- javascript - 如何为这种类型的构造函数创建原型?
- python - vim ALE “忽略” pyright
- redirect - 如何使用 sudo 将包含 $HOME 的文本简单地重定向到 /etc/zshenv?
- typescript - 在 webdriverio 中设置跨浏览器功能
- c++ - 如何将静态库捆绑到 NSIS 插件中
- computation-theory - 使用 Myhill-Nerode 证明语言的非正则性
- sql - 删除~~ sql之间的字符