這是我非常關心的第一個話題。 我仍在學習如何驗證,但我真的需要你的幫助。簡單驗證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
「它給了我大量的錯誤」 - 什麼錯誤? – Quentin 2014-10-06 08:41:54
你在哪裏定義了這個函數「renderForm」 – alagu 2014-10-06 08:44:31
好吧現在沒有錯誤。最後!但是,當我輸入所有表單時,錯誤消息仍然顯示「您的字段爲空」? – imn00b 2014-10-06 08:49:12