[英]How do I make the PHP form redirect to another url after successfully submitting the form?
[英]PHP redirect to another URL after submitting a form
我有一個包含基本信息(姓名,姓氏,出生日期,電話,電子郵件)的網絡表單
提交表單並將這些字段的數據插入MySQL數據庫后,我希望能夠重定向到其他URL。 我嘗試使用php的標頭函數,但由於某種原因,它不會將我重定向到我指定的URL。
<?php
if ($_POST) {
$host="23.229.187.201"; // Host name
$username="inturuser"; // Mysql username
$password="admin123"; // Mysql password
$db_name="intur"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
$link = mysql_connect("localhost", "inturuser", "hola123")or die("Error");
mysql_select_db("intur")or die("cannot select DB");
// Get values from form the form
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$bday = date('Y-m-d', strtotime($_POST['bday']));
$phone = $_POST['phone'];
$datetime = date('Y-m-d H:i:s');
$ip = "";
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
// Insert data into mysql
$sql = 'INSERT INTO users (nombre, apellido, correo, fecha_nacimiento, telefono, ip_restaurante, timestamp)
VALUES("'.$name.'", "'.$lastname.'", "'.$email.'", "'.$bday.'", "'.$phone.'", "'.$ip.'", "'.$datetime.'")';
mysql_query($sql);
mysql_close();
// if successfully insert data into database, displays message "Successful and redirect to a URL ".
if($result)
{
header('Location: http://www.example.com/');;
}
else {
echo "ERROR";
}
//close connection
}
else {
echo "NO POST";
}
?>
您尚未分配$result
變量,因此if($result)
始終為false。
您應該打開error_reporting
以顯示E_NOTICE,然后需要用$result = mysql_query($sql)
替換mysql_query
行。
您的代碼:
$sql = 'INSERT INTO users (nombre, apellido, correo, fecha_nacimiento, telefono, ip_restaurante, timestamp)
VALUES("'.$name.'", "'.$lastname.'", "'.$email.'", "'.$bday.'", "'.$phone.'", "'.$ip.'", "'.$datetime.'")';
mysql_query($sql);
mysql_close();
// if successfully insert data into database, displays message "Successful and redirect to a URL ".
if($result)
{
header('Location: http://www.example.com/');;
}
請從標題中刪除多余的分號。 並分配$result=mysql_query($sql)
更正的解決方案:
$sql = 'INSERT INTO users (nombre, apellido, correo, fecha_nacimiento, telefono, ip_restaurante, timestamp)
VALUES("'.$name.'", "'.$lastname.'", "'.$email.'", "'.$bday.'", "'.$phone.'", "'.$ip.'", "'.$datetime.'")';
$result=mysql_query($sql);
mysql_close();
// if successfully insert data into database, displays message "Successful and redirect to a URL ".
if($result)
{
header('Location: http://www.example.com/');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.