Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PEAR HTML_快速表单函数和数据处理_Php_Mysql_Pear - Fatal编程技术网

Php PEAR HTML_快速表单函数和数据处理

Php PEAR HTML_快速表单函数和数据处理,php,mysql,pear,Php,Mysql,Pear,我有以下代码,这是由PEAR的html快速表单生成的用户注册表单: <html> <head></head> <body> <?php require_once "HTML/QuickForm.php"; $form = new HTML_QuickForm('UserRegistration', 'POST'); //Form elements**************************

我有以下代码,这是由PEAR的html快速表单生成的用户注册表单:

 <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, '&nbsp;');

      //***********************************************************************************************************

      //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

我认为你可以通过几种不同的方式来实现,例如:

(1) PEAR数据对象:

if ($form->validate()) {
   $user = new DataObjects_User;
   $user->username = $form->exportValue('username');
   if($user->find()) {
      // username is already exists in the db
   }

}
(2) PDO


我希望上面的示例将提供如何检查数据库中是否已经存在值的方法。关于如何使用PEAR HTML_QuickForm有一个非常好的教程:

Vasil,谢谢你的回答,这两个示例是如何以代码方式实现到我发布的表单中的,这些PEAR包是要安装的吗?第二个问题是如何在验证后将数据插入数据库中?正如您所注意到的,我是php新手。顺便问一下,你是保加利亚人吗如果尚未安装MDB2,可以从命令行安装。此外,您还需要安装相应DBMS的驱动程序才能进行正常安装:$pear install MDB2$pear install MDB2#mysql,然后您应该在此处查看MDB2文档:Vasil,OK,аааааааааааааааааааааааа,这两个项目是作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为!,PEAR数据对象,MDB2。“pear安装MDB2”,即“pear安装MDB2”。В линка, който съм ти дал по-горе има детайлна информация как да извличаш или добавяш данни. 本机php是一种以php为基础的语言。本月上周五,梨园的一个月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日ъа空。
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);
    }

}