简单验证PHP

时间:2014-10-06 08:39:53

标签: php validation

这是我的第一个主题,所以请耐心等待。 我还在学习如何验证,但我真的需要你的帮助。

我需要验证名字,姓氏,街道,郊区,邮政编码,电子邮件,状态和出生日期。

如果我这样做:

if (empty($firstname)) {$errors[] =" First Name Can not be Empty <br> ";}
if (empty($lastname)) {$errors[] =" Last Name Can not be Empty <br> ";}
if (empty($street)) {$errors[] =" Street Can not be Empty <br> ";}
if (empty($suburb)) {$errors[] =" Suburb Can not be Empty <br> ";}
if (empty($postcode)) {$errors[] =" Postcode Can not be Empty <br> ";}
//    elseif (!is_numeric($postcode)) {$errors[] =" Postcode must be numeric ";}
    elseif(!preg_match("/\^\(\[0\-9\]\{5\}\(\[\-\\s\]\?\[0\-9\]\{4\}\)\?\)\$/", $postcode))  {$errors[] =" Please enter a valid post number <br> ";}
if( !preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i", $myemail) )  {$errors[] =" You have entered and invalid email address <br> ";}
if (empty($DOB)) {$errors[] =" Date only <br> ";}

它给了我很多错误。

这是我的完整PHP代码:

    <?php

 function renderForm($first, $last, $st, $sub, $pcode, $em, $sta, $dofb, $error)
 {
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <title>Add Student</title>
 </head>
 <body>
 <?php 

 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 echo "<p><b>Add new student details</b></p>";
 ?> 
 <form action="" method="post">
 <div>
 <strong>First name: </strong> <input type="text" name="FNAME" value="<?php echo $first; ?>" /><br/>
 <strong>Last name: </strong> <input type="text" name="LNAME" value="<?php echo $last; ?>" /><br/>
 <strong>Street: </strong> <input type="text" name="STREET" value="<?php echo $st; ?>" /><br/>
 <strong>Suburb: </strong> <input type="text" name="SUBURB" value="<?php echo $sub; ?>" /><br/>
 <strong>Postcode: </strong> <input type="text" name="POSTCODE" value="<?php echo $pcode; ?>" /><br/>
 <strong>Email: </strong> <input type="text" name="EMAIL" value="<?php echo $em; ?>" /><br/> 
 <strong>Status: </strong> <input type="text" name="STATUS" value="<?php echo $sta; ?>" /><br/>
 <strong>Date of Birth: </strong> <input type="text" name="DOB" value="<?php echo $dofb; ?>" /><br/>
 <p>Required Field</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 
 <p><a href="view.php">Update & delete students</a></p>
 </body>
 </html>
 <?php 
 }

 include('dbconnect.php');

 if (isset($_POST['submit']))
 { 
 $firstname = mysql_real_escape_string(htmlspecialchars($_POST['FNAME']));
 $lastname = mysql_real_escape_string(htmlspecialchars($_POST['LNAME']));
 $street = mysql_real_escape_string(htmlspecialchars($_POST['STREET']));
 $suburb = mysql_real_escape_string(htmlspecialchars($_POST['SUBURB']));
 $postcode = mysql_real_escape_string(htmlspecialchars($_POST['POSTCODE']));
 $email = mysql_real_escape_string(htmlspecialchars($_POST['EMAIL']));
 $status = mysql_real_escape_string(htmlspecialchars($_POST['STATUS']));
 $dob = mysql_real_escape_string(htmlspecialchars($_POST['DOB']));

 if ($firstname == '' || $lastname == '' || $street == '' || $suburb == '' || $postcode == '' || $email == '' || $status == '' || $dob == '')
 {

 $error = 'Your field is empty';

 renderForm($firstname, $lastname, $street, $suburb, $postcode, $email, $status, $dob, $error);
 }
 else
 {

 mysql_query("INSERT student SET FNAME='$firstname', LNAME='$lastname', STREET='$street', SUBURB='$suburb', POSTCODE='$postcode', EMAIL='$email', STATUS='$status', DOB='$dob' ")
 or die(mysql_error()); 

 header("Location: view.php"); 
 }
 }
 else

 {
 renderForm('','','','','','','','','');
 }
?>

我不知道该做什么,我真的很困惑,所以我想知道你是否可以帮助我?是的,我到处搜索并在本网站上关注一些教程甚至答案,但它不起作用。我还在苦苦挣扎。 非常感谢您的帮助! 谢谢。

编辑:链接:http://viper-7.com/HfyXw3 dbconnect的链接:http://viper-7.com/14PG1H

3 个答案:

答案 0 :(得分:0)

尝试制作一个字段数组:

$fields = array(
    'fname' => $_POST['fname'],
    'lname' => $_POST['lname'],
    'street' => $_POST['street'],
    'suburb' => $_POST['suburb'],
    'postcode' => $_POST['postcode'],
    'email' => $_POST['email'],
    'status' => $_POST['status'],
    'dob' => $_POST['dob']
);

foreach( $fields as $field )
{
    if( empty($field) )
    {
        $errors[] = "Your field is empty";
    }
}

答案 1 :(得分:0)

我建议的所有内容:http://viper-7.com/3JSTyP现在应该可以使用,但无法测试

为什么要用renderForm清空表单?从页面顶部删除:

<?php
    function renderForm($first, $last, $st, $sub, $pcode, $em, $sta, $dofb, $error) {
?>

删除第二个PHP脚本开头的}

同时删除最后的else语句:

else {
    renderForm('','','','','','','','','');
}

然后最终放入表单的动作部分:

"<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"

编辑:

添加(位于页面顶部):

$error = $firstname = $lastname = $street = $suburb = $postcode = $email = $status = $dob = '';

声明变量,然后在表单中更改每个变量以匹配变量(例如):

$first = $firstname

答案 2 :(得分:0)

您不能简单地验证街道名称,因为它可能因国家/地区或城市而异! 所以我认为只是使用:

$firstname = mysql_real_escape_string(htmlspecialchars($_POST['FNAME']));
$lastname = mysql_real_escape_string(htmlspecialchars($_POST['LNAME']));
$street = mysql_real_escape_string(htmlspecialchars($_POST['STREET']));
$suburb = mysql_real_escape_string(htmlspecialchars($_POST['SUBURB']));

用于验证您可以使用的电子邮件:

preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email);

并且对于验证邮政编码,它又取决于您想要检查的国家/地区! 这些正则表达式可以帮助你:

Afghanistan,Angola,Belize,Benin,Hong Kong,Ireland, Macau
no Postal Code

Argentina
^[A-Z][0-9]{4}[A-Z]{3}$

Canada
^(?!.*[DFIOQU].*)([A-Z][0-9]){3}$       

US
^[\d]{5}(-[\d]{4})?$

UK
^[A-Z]?[A-Z][0-9][A-Z0-9]?\s[0-9][A-Z]{2}$


Latvia
^LV[-\s]?[\d]{4}$

Hungary,Denmark,Cyprus,Georgia,Bangladesh,Austria,Armenia,Australia,Albania,Belgium,Bulgaria,Cape   Verde,Philippines,Paraguay
Norway,New Zealand,Liechtenstein,Luxembourg,South Africa,Tunisia,Switzerland
^[\d]{4}$

Netherlands
^[\d]{4}\s[A-Z]{2}$

Portugal
^[\d]{4}[\s-][\d]{3}$

Israel,Iraq,Indonesia,Greece,Germany,Guam,Croatia,Costa    Rica,Estonia,Egypt,France,Finland,American Samoa,Algeria
 Brazil,Bosnia and Herzegovina,Cambodia,Palau,Morocco,Montenegro,Northern Mariana  Islands,Lithuania,Italy,Malaysia
 Mexico,Marshall Islands,Micronesia,Serbia,Puerto Rico,San Marino,Taiwan,Thailand,Spain,Sri L  anka,Turkey,Ukraine,U.S. Virgin Islands,Vatican
^[\d]{5}$

 Poland
 ^[\d]{2}[\s-]?[\d]{3}$

Czech Republic, Slovakia, Sweden
^[\d]{3}[\s-]?[\d]{2}$

Iran
^[\d]{5}[\s-][\d]{5}$


China,Colombia,Belarus,Panama,Pakistan,Nigeria,Kazakhstan,Singapore,Romania,Russia
^[\d]{6}$

只是带有这些模式的preg_match()会进行验证!