PHP URL重定向无法按预期工作

时间:2013-10-27 04:16:13

标签: php html url header

我在提交表单后加载新的“thankyou.php”文件时遇到问题。表单将数据发送给自己,如果所有数据都通过验证,则会继续将数据保存在数据库中。

“thankyou.php”完全加载,但“signup.php”仍然存在,两个文件的输出仍保留在同一页面中。我希望它在一个新页面上,但它不起作用。

这是我的卫生设施:

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["firstName"]))
{$Err[] = "* First Name is required";}
else
{
$name = test_input($_POST["firstName"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name))
  {
  $Err[] = "Only letters are allowed in First Name"; 
  }
}

if (empty($_POST["email"]))
{$Err[] = "* Email is required";}
else
{
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
  {
  $Err[] = "Invalid email format"; 
  }
}   
}

形式:

<div id = "signupform">

“&gt;                  电子邮件地址                  名字
                   

数据库连接和插入:

try {
  $conn = new PDO('mysql:host=localhost; dbname=mydb', 'root', ''); 
  $conn->setAttribute(PDO:: ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

catch(PDOException $ pe){       echo('连接错误,因为:'。$ pe-&gt; getMessage());     }

//如果值不为空且已清理,则将数据插入数据库   if(!empty($ _ POST [“firstName”])&amp;&amp;!empty($ _ POST [“email”]))   {     $ qry =“INSERT INTO userdetails(email,firstName)值(?,?)”;

$q = $conn->prepare($qry) or die("ERROR: " . implode(":", $conn->errorInfo()));

$q->bindParam(1, $email);
$q->bindParam(2, $firstName);


try {
  if($q->execute()){
    header("Location: invoice.php");
    exit;
  }
  }
catch(PDOException $pe) {
  echo('Connection error, because: ' .$pe->getMessage());
}

}

这就是全部,但它仍然没有加载新的页面。

2 个答案:

答案 0 :(得分:0)

我认为问题是你要拨打$q->execute()两次,试试这个:

try {
    if ($q->execute()){
        header("Location: thankyou.php");
        exit;
    }
} catch(PDOException $pe) {
    echo('Connection error, because: ' .$pe->getMessage());
}

答案 1 :(得分:0)

在进一步深入挖掘并更多地了解你实际在做什么之后(错误的方式),我提出了我的解决方案。几乎你需要做到这一点,以便MySQL插入语句是错误验证的一部分,而不是孤立的。如果你查看我之前的代码,PDO语句在代码中没有真正的位置,就在那里。

但是,如果确实没有$Err,它将运行插入脚本并对该脚本进行错误检查。然后,如果它正确插入,它将把用户重定向到下一页。

对于验证码,请使用 submit 按钮进行检查。

if(isset($_POST["submit"])){if (empty($_POST["firstName"])){$Err[] = "* First Name is required";}else{$name = test_input($_POST["firstName"]);if (!preg_match("/^[a-zA-Z ]*$/",$name))  {  $Err[] = "Only letters are allowed in First Name";   }}if (empty($_POST["email"])){$Err[] = "* Email is required";}else{$email = test_input($_POST["email"]);if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){  $Err[] = "Invalid email format";   }}   }

验证错误消息:

if (empty($Err) === false) {
        echo '<h3>';
        foreach ($errors as $Err) {
            echo '<center><li>', $error, '</li></center>';
        }

        echo '< h3 >';
    }

您的HTML表单应如下所示:

<form method="post"  action ="echo $_SERVER['PHP_SELF'];"><table >        
<tr><td>Email Address < input type="text"  name="email" value="email" ></td>
</tr><tr valign="baseline"> 
  <td>First Name <br/>< input type="text" name="firstName" value="" ></td>
</tr>
<input type="submit" value="submit" name="submit">
</form>

现在,服务器端查询操作将仅通过验证执行:

try {
    $conn = new PDO('mysql:host=localhost; dbname=mydb', 'root', ''); 
    $conn->setAttribute(PDO:: ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $pe) {
    echo('Connection error, because: ' .$pe->getMessage());
}if (!empty($_POST["firstName"]) && !empty($_POST["email"])){
$qry = "INSERT INTO userdetails (email, firstName) values (?, ?)";
$q = $conn->prepare($qry) or die("ERROR: " . implode(":", $conn->errorInfo()));
$q->bindParam(1, $email);
$q->bindParam(2, $firstName);
try {
    if($q->execute()){

// You can use Two Way to Redire Server side Depending on version

       // http_redirect("invoice.php");
       //OR (Above & below any 1 line can be workout for you.
        header("Location: invoice.php");
        exit;
    }
    }
catch(PDOException $pe) {
    echo('Connection error, because: ' .$pe->getMessage());
}}