从数据库中的表单插入数据后,页面重定向到主页

时间:2016-03-15 08:51:04

标签: php html forms mysqli

我不知道为什么在从表单向数据库提交数据后,我的页面会重定向到我的主页。数据是成功提交的,而是提交到我的主页的页面,即www.xxxx.com

重要细节已更改为XXX

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title></title>

</head>

<body>

<?php

$servername = "localhost";
$username = "XXXX";
$password = "XXXXX";
$dbname = "XXXX_officetest";

// Create connection
$conn = mysql_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_error());
}

mysql_select_db($dbname, $con);

// Escape user inputs for security
$cx_name = mysql_real_escape_string( $_POST['cxinputname']);
$cx_email = mysql_real_escape_string( $_POST['cxinputemail']);
$cx_phone = mysql_real_escape_string( $_POST['cxinputphone']);
$cx_key=mysql_real_escape_string( $_POST['cxinputkey']);
$cx_location=mysql_real_escape_string( $_POST['cxinputlocation']);

$sql = "INSERT INTO `xxxxx`.`cux_details` (`cx_name`, `cx_email`, `cx_phone`, `cx_key`, `cx_location`)
VALUES ('$cx_name', '$cx_email', '$cx_phone' ,'$cx_key' ,'$cx_location')";

$result=mysql_query($sql);

if($result){
header('Location: http://www.xxxxx.com/submit.html'); //redirect
exit();
}
else{
header('Location: http://www.xxxxxx.com/submit.html'); //redirect
exit();
}


mysql_free_result($result);
?>

</body>
</html>

2 个答案:

答案 0 :(得分:2)

您需要在输出任何HTML之前运行PHP代码,因此您的代码将是:

<?php
$servername = "localhost";
$username = "XXXX";
$password = "XXXXX";
$dbname = "XXXX_officetest";

// Create connection
$conn = mysql_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_error());
}

mysql_select_db($dbname, $con);

// Escape user inputs for security
$cx_name = mysql_real_escape_string( $_POST['cxinputname']);
$cx_email = mysql_real_escape_string( $_POST['cxinputemail']);
$cx_phone = mysql_real_escape_string( $_POST['cxinputphone']);
$cx_key=mysql_real_escape_string( $_POST['cxinputkey']);
$cx_location=mysql_real_escape_string( $_POST['cxinputlocation']);

$sql = "INSERT INTO `xxxxx`.`cux_details` (`cx_name`, `cx_email`, `cx_phone`, `cx_key`, `cx_location`)
VALUES ('$cx_name', '$cx_email', '$cx_phone' ,'$cx_key' ,'$cx_location')";

$result=mysql_query($sql);

if($result){
header('Location: http://www.xxxxx.com/submit.html'); //redirect
//exit();
}
else{
header('Location: http://www.xxxxxx.com/submit.html'); //redirect
exit();
}


mysql_free_result($result);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title></title>

</head>

<body>

Content goes here...

</body>
</html>

答案 1 :(得分:0)

 <form class="form-horizontal cxform" action="http://www.xxxxxx.com/cx_mail_send.php" method="post">


<input required="required" type="text" class="form-control" id="cxinputname" name="cxinputname">
<input required="required" type="email" class="form-control" id="cxinputemail" name="cxinputemail">
<input required="required" type="tel" class="form-control" id="cxinputphone" maxlength="10" name="cxinputphone">
<input required="required" type="text" class="form-control" id="cxinputkey" name="cxinputkey" maxlength="30">
<select class="form-control" name="cxinputlocation">
                        <option value="">Choose Country</option>

<button type="submit" class="btn" id="cxsubmit" value="Submit">Get Started</button>

</form>

选项值有很多价值,所以没有post.This是我的表单调用我的cx_mail_send.php.Hope它帮助