当我提交一个form.php它调用insert.php。 insert.php将表单数据插入到数据库中,并回显“成功添加记录”消息。我希望在显示此消息后,自动调用返回到form.php。我还评论了我必须重定向的行。 想要自动重定向到一些页面在php
My form.php
<html>
<body>
<form action="insert.php" method="post">
Name: <input type="text" name="myname"><br>
Comment: <input type="text" name="mycomment"><br>
<input type="submit">
</form>
</body>
</html>
My insert.php
<?php
$con=mysqli_connect("localhost","root","","commentdb");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$firstname = mysqli_real_escape_string($con, $_POST['myname']);
$lastname = mysqli_real_escape_string($con, $_POST['mycomment']);
$sql="INSERT INTO person (Name, Comment, Time)
VALUES ('$firstname', '$lastname', NOW())";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
// Here it should retrieve
mysqli_close($con);
?>
您可以使用标题,但是您也将无法回显消息,因为这将在标题之前输出。如果你想同时回声和重定向,你将需要使用JS或元刷新。 – 2014-09-25 08:00:40