我有以下代码,它是由PEAR的html-Quick Form生成的用户注册表单:
<html>
<head></head>
<body>
<?php
require_once "HTML/QuickForm.php";
$form = new HTML_QuickForm('UserRegistration', 'POST');
//Form elements***********************************************
form->addElement('header', 'RegistrationHeader', 'Fill in your details');
$form->addElement('text', 'firstname', 'First Name', array('size' => 49, 'maxlength'=>49));
$form->addElement('text', 'lastname', 'Last Name', array('size' => 49, 'maxlength'=>49));
$form->addElement('text', 'username', 'Username',array('size'=> 49, 'maxlength'=>49));
$form->addElement('password', 'password', 'Password', array('size' => 30, 'maxlength'=>30));
$form->addElement('password', 'confirmpassword', 'Confirm Password', array('size'=> 30, 'maxlength'=>30));
$form->addElement('text', 'email', 'Email', array('size'=> 49, 'maxlength'=>49));
$form->addElement('text', 'confirmemail', 'Confirm Email', array('size'=>49, 'maxlength'=>49));
$form->addElement('hidden', 'ip', $_SERVER['REMOTE_ADDR']);
$buttons[] = &HTML_QuickForm::createElement('reset', 'null', 'Clear');
$buttons[] = &HTML_QuickForm::createElement('submit', 'null', 'Submit');
$form->addGroup($buttons, null, null, ' ');
//***********************************************************************************************************
//Setting of form functions*************************************************
//Email DNS check function
function checkEmailDNS($email, $domainCheck = false)
{
if (preg_match('/^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+'.
'\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $email)) {
if ($domainCheck && function_exists('checkdnsrr')) {
list (, $domain) = explode('@', $email);
if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) {
return true;
}
return false;
}
return true;
}
return false;
}
$form->registerRule('checkmailDNS', 'callback', 'checkEmailDNS');
//******************************************************************************************
//Check for Email existance in the DataBase
function checkEmailfromDB($value)
{
try {
$pdo = new PDO('mysql:dbname=mytestsite;host=localhost', 'root', '');
} catch (PDOException $e) {
die('ERROR: Cannot connect: ' . $e->getMessage());
}
$value = $pdo->quote($value);
$sql = "SELECT userID FROM users WHERE userEMail= $value;";
$ret = $pdo->query($sql) or die('ERROR: ' . implode(':', $pdo->errorInfo()));
$str = $ret->fetchColumn();
$flag = ($str == false) ? true : false;
unset($pdo);
return $flag;
}
//*******************************************************************
//****************Check for username existance in the DataBase
function checkusernamefromDB($value)
{
try {
$pdo = new PDO('mysql:dbname=mytestsite;host=localhost', 'root', '');
} catch (PDOException $e) {
die('ERROR: Cannot connect: ' . $e->getMessage());
}
$value = $pdo->quote($value);
$sql = "SELECT userID FROM users WHERE userName= $value;";
$ret = $pdo->query($sql) or die('ERROR: ' . implode(':', $pdo->errorInfo()));
$str = $ret->fetchColumn();
$flag = ($str == false) ? true : false;
unset($pdo);
return $flag;
}
//**************************************************
//Form Rules********************************************
$form->addRule('firstname', 'Your First name is required', 'required');
$form->addRule('lastname', 'Your Last name is required', 'required');
$form->addRule('username', 'Username is required', 'required');
$form->addRule('úsername', 'Username is already in use, choose a different one', 'callback', 'checkusernamefromDB');
$form->addRule('password', 'Error: Enter a password', 'required');
$form->addRule('password', 'Error: The password should be at least 6 characters long', 'rangelength', array(6,30));
$form->addRule('confirmpassword', 'Error: Password confirmation is required', 'required');
$form->addRule('confirmpassword', 'Error: The password should be at least 6 characters long', 'rangelength', array(6,30));
$form->addRule(array('password','confirmpassword'), 'ERROR: Password mismatch', 'compare');
$form->addRule('email', 'Emal is required', 'required');
$form->addRule('email', 'Enter a valid Email adress', 'email');
$form->addRule('email', 'Email is incorrect', 'checkmailDNS', true);
$form->addRule('email','Email is already in use on the system', 'callback', 'checkEmailfromDB');
$form->addRule('confirmemail','Email confirmation is required','required');
$form->addRule('confirmemail', 'Email is incorrect', 'checkmailDNS', true);
$form->addRule('email','Email is already in use on the system', 'callback', 'checkEmailfromDB');
$form->addRule(array('email', 'confirmemail'), 'Error: Email mismatch', 'compare');
//***********************************************************************************************************
//Form Filters*********************************************************************************************
$form->applyFilter('_ALL_', 'trim');
$form->applyFilter('firstname', 'lettersonly');
$form->applyFilter('firstname', 'strtolower');
$form->applyFilter('firstname', 'ucfirst');
$form->applyFilter('lastname', 'lettersonly');
$form->applyFilter('lastname', 'strtolower');
$form->applyFilter('lastname', 'ucfirst');
$form->applyFilter('email', 'strtolower');
$form->applyFilter('confirmemail', 'strtolower');
//Display Form************************************************************
if ($form->validate()) {
$form->freeze();
}
$form->display();
?>
</body>
</html>
检查数据库中是否已存在某个值的正确方法是什么,如果确实存在,您知道 - 作为验证规则,并且重要的是 - 处理数据并将其插入的方式是什么数据库?我对PHP很新,对PEAR来说更是如此,我看过一些手册,但它们似乎并没有显示提交后的数据是如何处理的,可能是一件事的新手......:D
答案 0 :(得分:0)
我认为你可以通过几种不同的方式做到这一点,例如:
(1)PEAR DataObject:
if ($form->validate()) {
$user = new DataObjects_User;
$user->username = $form->exportValue('username');
if($user->find()) {
// username is already exists in the db
}
}
(2)PDO
if ($form->validate()) {
$username = $form->exportValue('username');
$email = $form->exportValue('password');
if( !checkusernamefromDB($username) or !checkEmailfromDB($email) ) {
$form->display();
} else {
$sql = "INSERT INTO users (userID,userEMail) VALUES($username, $email)";
$pdo->query($sql);
}
}
我希望以上示例能够了解如何检查数据库中是否已存在某个值。有一个非常好的教程如何使用PEAR HTML_QuickForm:http://devzone.zend.com/1165/generating-and-validating-web-forms-with-pear-html_quickform-part-2/