Php 我的注册表中的错误输出
在运行SQL查询之前,我尝试创建一个注册表单并进行一些检查,但是当我测试并尝试生成多个错误时,我只得到了第一个错误,有时甚至根本没有错误。我找不到我犯错误的地方。 下面是PHP中的代码Php 我的注册表中的错误输出,php,Php,在运行SQL查询之前,我尝试创建一个注册表单并进行一些检查,但是当我测试并尝试生成多个错误时,我只得到了第一个错误,有时甚至根本没有错误。我找不到我犯错误的地方。 下面是PHP中的代码 //function to filter only phone numbers function get_phone($number) { return preg_replace('#[^0-9]#', '', $number); } //function to take only alphabets.
//function to filter only phone numbers
function get_phone($number) {
return preg_replace('#[^0-9]#', '', $number);
}
//function to take only alphabets.
function get_alpha($alphabets){
return preg_replace('#[^a-z]#', '', $alphabets);
}
//function to check email.
function isValidEmail($email){
if (strlen ($email) > 50){
$errors[] = 'email address too long, please use a shorter email address..!';
} else {
return (filter_var($email, FILTER_VALIDATE_EMAIL));
}
}
function output_errors($errors){
$output = array();
foreach($errors as $error) {
$output[] = '<li>' . $error . '</li>';
}
return '<ul>' . implode('', $output) . '</ul>';
}
if (empty($_POST) === false) {
//store the text box field names of the form to local variables.
$cust_name = $_POST['name1'];
$cust_email = $_POST['email'];
$cust_phone = $_POST['phone'];
$cust_addr1 = $_POST['addr1'];
$cust_addr2 = $_POST['addr2'];
$cust_city = $_POST['city'];
$cust_state = $_POST['state'];
$cust_country = $_POST['country'];
$username = $_POST['uname'];
$password = $_POST['passwd'];
$cnf_passwd = $_POST['cnf_passwd'];
$sec_que = $_POST['sec_que'];
$sec_ans = $_POST['sec_ans'];
//sanitize the inputs from the users end.
$cust_name = sanitize($username);
$cust_phone = get_phone($cust_phone);
$cust_addr1 = sanitize($cust_addr1);
$cust_addr2 = sanitize($cust_addr2);
$cust_city = get_alpha($cust_city);
$cust_state = get_alpha($cust_state);
$cust_country = get_alpha($cust_country);
$username = sanitize($username);
$password = md5($password);
$cnf_passwd = md5($cnf_passwd);
$sec_que = sanitize($sec_que); //put up dropdown menu
$sec_ans = sanitize($sec_ans);
$cust_email = isValidEmail($cust_email);
//check for error handling in form data
//1. check for empty fields,
if ($cust_name == "" || $cust_phone == "" ||
$cust_addr1 == "" || $username == "" ||
$password == "" || $cnf_passwd == "" ||
$sec_que == "" || $sec_ans == ""
) {
$errors[] = 'No blank fields allowed, please fill out all the required fields..!';
//2.check for field lengths
} else if (strlen($cust_name) < 3 || strlen($cust_name > 20)) {
$errors[] = 'The name length should be between 3 to 20, please check & correct..!';
//3. check for phone number length
} else if (strlen($cust_phone) < 10 || strlen($cust_phone) > 11) {
$errors[] = 'The phone number must be 10 or 11 digits..!';
//4. check for address input lengths.
} else if (strlen($cust_addr1) < 5 || strlen($cust_addr1) > 50) {
$errors[] = 'Please provide a valid address..to serve you better..!';
//5. check if the password fields content match.
//length is not checked because the entered values will be converted to MD5 hash
// of 32 characters.
} else if ($password != $cnf_passwd) {
$errors[] = 'The passwords do not match. Please enter your passwords again..!';
// 6. check for length of the security answers.
} else if (strlen($sec_ans) < 5 || strlen($sec_ans) > 50) {
$errors[] = 'Please enter a proper security answer..!';
} //7. check for valid email address
else if($cust_email == false){
$errors[] = 'The email address you entered is not valid, please check and correct..!';
} else {
execute the SQL queries and enter the values in the database.
echo 'GOOD...TILL NOW..!!!';
}
} else {
$errors [] = 'No data received, Please try again..!!';
}
if(empty($errors) === false) {
?>
<h2>The Following errors were encountered:</h2>
<?php
echo output_errors($errors); //output the errors in an ordered way.
}
?>
//只过滤电话号码的函数
函数get_phone($number){
返回preg#u replace(“#[^0-9]#,”$number);
}
//函数仅获取字母表。
函数get_alpha($alphabets){
返回preg#u replace(“#[^a-z]#,”$alphabets);
}
//功能检查电子邮件。
函数isValidEmail($email){
if(斯特伦($email)>50){
$errors[]=“电子邮件地址太长,请使用较短的电子邮件地址…”;
}否则{
返回(filter_var($email,filter_VALIDATE_email));
}
}
函数输出错误($errors){
$output=array();
foreach($errors作为$error){
$output[]=“”.$error. ”;
}
返回“”。内爆(“”,$output)。“
”;
}
if(空($\u POST)==false){
//将表单的文本框字段名称存储到局部变量。
$cust_name=$_POST['name1'];
$cust_email=$_POST['email'];
$cust_phone=$_POST['phone'];
$cust_addr1=$u POST['addr1'];
$cust_addr2=$u POST['addr2'];
$cust_city=$_POST['city'];
$cust_state=$_POST['state'];
$cust_country=$_POST['country'];
$username=$_POST['uname'];
$password=$_POST['passwd'];
$cnf_passwd=$_POST['cnf_passwd'];
$sec_que=$_POST['sec_que'];
$sec_ans=$_POST['sec_ans'];
//清理来自终端用户的输入。
$cust_name=消毒($username);
$cust\u phone=get\u phone($cust\u phone);
$cust_addr1=消毒($cust_addr1);
$cust_addr2=消毒($cust_addr2);
$cust_city=get_alpha($cust_city);
$cust_state=get_alpha($cust_state);
$cust\u country=get\u alpha($cust\u country);
$username=sanitize($username);
$password=md5($password);
$cnf_passwd=md5($cnf_passwd);
$sec_que=消毒($sec_que);//打开下拉菜单
$sec_ans=消毒($sec_ans);
$cust_email=isValidEmail($cust_email);
//检查表单数据中的错误处理
//1.检查是否存在空字段,
如果($cust_name==“”| |$cust_phone==“”)||
$cust_addr1==“”| |$username==“”| |
$password==“”| |$cnf|U passwd==“”| |
$sec_que==“”| |$sec_ans==“”
) {
$errors[]=“不允许空白字段,请填写所有必填字段..!”;
//2.检查字段长度
}else if(斯特伦($cust|u name)<3 |斯特伦($cust|u name>20)){
$errors[]='名称长度应介于3到20之间,请检查并更正..!';
//3.检查电话号码长度
}如果(斯特伦($cust|U电话)<10 |斯特伦($cust|U电话)>11){
$errors[]=“电话号码必须是10或11位数字…”;
//4.检查地址输入长度。
}如果(斯特伦($cust|u addr1)<5 |斯特伦($cust|u addr1)>50){
$errors[]='请提供有效地址..以便更好地为您服务..!';
//5.检查密码字段内容是否匹配。
//未检查长度,因为输入的值将转换为MD5哈希
//共32个字符。
}else if($password!=$cnf_passwd){
$errors[]='密码不匹配。请重新输入密码。!';
//6.检查安全答案的长度。
}如果(斯特伦($sec|ans)<5 |斯特伦($sec|ans)>50){
$errors[]=“请输入正确的安全答案…”;
}//7.检查有效的电子邮件地址
else if($cust_email==false){
$errors[]=“您输入的电子邮件地址无效,请检查并更正…”;
}否则{
执行SQL查询并在数据库中输入值。
回音‘很好……到现在为止……!’;
}
}否则{
$errors[]=“未收到数据,请重试…”;
}
if(空($errors)==false){
?>
遇到以下错误:
使用此结构时:
if () {
} else if () {
} else if () {
}
// etc.
然后只能满足一个条件。只要其中一个if
条件为真,其余的else if
块和最后的else
块将被忽略
如果您的条件不是相互排斥的,请将它们放在各自单独的块中:
if () {
}
if () {
}
if () {
}
// etc.
非常感谢david…将更正代码并进行测试。学到了重要的一课。