很大程度上基于此电子邮件验证代码(有效 - 我已经测试过)
<?php
require_once("include/connectionpdo.php");
echo("<h2>parsed1</h2> ");
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
echo "If you have fixed any possible errors, and you believe that there is a problem with our submission system, please ";
die();
}
echo("<h2>parsed2</h2> ");
// first checks to see if any information is supplied at all in the required fields
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['addressl1']) ||
!isset($_POST['addressl2']) ||
!isset($_POST['town']) ||
!isset($_POST['county']) ||
!isset($_POST['type']))
{
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
echo("<h3>parsed3</h3> ");
$first_name = $_POST['fname']; // required
$last_name = $_POST['sname']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$dateofbirth = $_POST['dob']; // required
$addresslone = $_POST['addressl1']; // required
$addressltwo = $_POST['addressl2']; // required
$townnm = $_POST['town']; // required
$countynm = $_POST['county']; // required
$typeapp = $_POST['type']; // required
$issubscribed = $_POST['subscribed']; // required
// checks to see if information supplied is correct
echo("<h4>parsed4</h4> ");
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$number_exp = '/^[0-9]/';
if(!preg_match($email_exp,$email_from)) {
echo("errortest1 ");
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
echo("errortest2 ");
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
echo("errortest3 ");
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
$number_exp = '/^[0-9]/';
if(!preg_match($string_exp,$last_name)) {
echo("errortest4 ");
$error_message .= 'Please just enter numerical values for your phone number.<br />';
}
if(strlen($addresslone) < 2) {
echo("errortest5 ");
$error_message .= 'The address (line one) you entered do not appear to be valid.<br />';
}
if(strlen($addressltwo) < 2) {
echo("errortest6 ");
$error_message .= 'The address (line two) you entered do not appear to be valid.<br />';
}
if((!preg_match($string_exp,$townnm)) || (strlen($townnm) < 2)) {
echo("errortest7 ");
$error_message .= 'The town you entered does not appear to be valid.<br />';
}
if((!preg_match($string_exp,$countynm)) || (strlen($countynm) < 2)) {
echo("errortest8 ");
$error_message .= 'The county you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
# the data we want to insert
$data = array($first_name, $second_name, $email_from, $telephone, $dateofbirth, $addresslone, $addressltwo, $townnm, $countynm, $typeapp, $issubscribed);
$STH = $DBH->prepare("INSERT INTO members (fname, sname, email, phone, dob, addressl1, addressl2, town, county, type, subscribed) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$STH->execute($data);
?>
<!--<!DOCTYPE html>
<head><title></title></head><body> -->
Thank you for contacting us We will be in touch with you very soon.
<!-- </body></html> -->
代码打印出“parsed 2”,但并没有走得更远。
function dead 运行,但仅打印附加的通用消息“'我们很抱歉,但您提交的表单似乎有问题。”
奇怪的是 !isset 似乎总是返回 true (不管表单中的表单信息是什么)。我删除了所有 if !isset 块,这导致打印“parsed 3”、“parsed 4”、“errortest1”和“您输入的电子邮件地址似乎无效”。同样,这与表单中的电子邮件地址是否符合规定条件无关。删除 if!preg_match 语句会导致生成错误消息
“致命错误:在第 88 行的 public_html/application4.php 中的非对象上调用成员函数 prepare()”
.
没有语法错误,或者至少如果有任何语法错误,它们只会产生逻辑错误。