主要基于this email validation code(有效 - 我已经测试过了)
<?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> -->
代码打印“解析为2”,但没有进一步深入。
函数已停止运行,但仅打印另外的通用消息“'我们很抱歉,但您提交的表单似乎有问题。”
奇怪!isset似乎总是返回true(无论表单中的表单信息是什么)。我删除了所有if!isset块,导致打印“已解析3”,“解析4”,“errortest1”和“您输入的电子邮件地址似乎无效”。同样,这与表格中的电子邮件地址是否符合规定的条件无关。删除if!preg_match语句会导致生成错误消息
“致命错误:在非对象中调用成员函数prepare() 第88行的“public_html / application4.php”
没有语法错误,或者至少如果有任何语法错误,它们只会产生逻辑错误。
答案 0 :(得分:1)
imho您已更改first_name
和last_name
字段的名称,因此如果条件中的所有字段都已设置,则代码应返回false:
if(!isset($_POST['fname']) ||
!isset($_POST['sname']) ||
!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.');
}
答案 1 :(得分:1)
在代码中查看差异或变量名称。
在echo("<h2>parsed2</h2> ");
之后
你已经用过了
!isset($_POST['first_name']
以及echo("<h3>parsed3</h3> ");
你已经用过了
$first_name = $_POST['fname']; // required
“ first_name ”如何变为“ fname ”?
要么将first_name用于代码,要么将fname用于代码。
与此相同, last_name 变为 sname 。更新此代码并再次测试。