2014-10-06 103 views
0

这是我非常关心的第一个话题。 我仍在学习如何验证,但我真的需要你的帮助。简单验证PHP

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

如果我这样做是这样的:

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

+0

“它给了我大量的错误” - 什么错误? – Quentin 2014-10-06 08:41:54

+0

你在哪里定义了这个函数“renderForm” – alagu 2014-10-06 08:44:31

+0

好吧现在没有错误。最后!但是,当我输入所有表单时,错误消息仍然显示“您的字段为空”? – imn00b 2014-10-06 08:49:12

回答

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"; 
    } 
} 
+0

我不知道该把它放在哪里,也不知道要删除我的其他代码。 – imn00b 2014-10-06 09:31:59

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 
+0

我做了你告诉我和这个想法: http://prntscr.com/4tj2bo – imn00b 2014-10-06 09:24:53

+0

@ imn00b现在就试试。 – 2014-10-06 09:41:18

+0

只是做了这个,仍然是一样的错误。我很沮丧... – imn00b 2014-10-06 09:45:22

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); 

以及验证码后,它agains取决于哪个国家你想检查! 这些正则表达式可以帮助你:

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()与这些模式将做您的验证!