0

提交表单后,我在加载新的“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">

" > 电子邮件地址名字

和数据库连接和插入:

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

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

//如果值不为空并经过清理,则将数据插入数据库 if (!empty($_POST["firstName"]) && !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());
}

}

仅此而已,但它仍未加载新页面。

4

2 回答 2

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());
}}
于 2013-10-27T06:45:34.620 回答
0

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

try {
    if ($q->execute()){
        header("Location: thankyou.php");
        exit;
    }
} catch(PDOException $pe) {
    echo('Connection error, because: ' .$pe->getMessage());
}
于 2013-10-27T04:20:01.493 回答