我有一个基本信息的网络表单(姓名,姓氏,姓名,出生日期,电话,电子邮件)
提交表单并将这些字段的数据插入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";
}
?>
答案 0 :(得分:3)
您尚未分配$result
变量,因此if($result)
始终为false。
您应该打开error_reporting
以显示E_NOTICE,然后您需要将mysql_query
行替换为$result = mysql_query($sql)
。
答案 1 :(得分:0)
您的代码:
$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/');
}