PHP OOP - 重复记录条目

时间:2017-08-08 02:13:16

标签: php mysql class oop switch-statement

我必须在我的mysql数据库中使用表格 恢复 enter image description here

我正在使用PHP OOP我有这个用户需要填写的 register.php 表单。

我的目标是:

  1. 只要位置不发送两次,同一用户就可以提交多个申请。例如“Thomas使用 thomas@mail.com 发送第一个应用程序,ID 1111 ,并将其定位为 执行 但他不能使用相同的电子邮件地址或身份证号码第二次提交相同的职位“
  2. 有人可以帮忙吗?以下是我的代码。 *注意:我在 Check.php 时遇到问题。我不知道如何检查 案例'重复': 区域。

    Register.php

    <div class="makeSquare"></div>
    <p>This is a square.</p>

    Check.php

    <?php
        require_once 'database/connect.php';
        if(isset($_POST['Submit'])) {
            $filter         = new Check();
            $submission     = $filter->filterForm($_POST, array(
                'email'             => array(
                        'required'  => true,
                        'unik'      => 'resumes'
                    ),
                'id_number'         => array(
                        'required'  => true,
                        'unik'      => 'resumes'
                    ),
                'positions'         => array(
                        'required'  => true,
                        'duplicate' => 'resumes'
                    )
            ));
            if($submission->valid()){
                $sender = new Sender();
                try{
                    $sender->create(array(
                        'email'         => Input::get('email'),
                        'id_number'     => Input::get('id_number'),
                        'positions'     => Input::get('positions')
                    ));
                    // header to other location after success
                }catch(Exception $e){
                    die($e->getMessage());
                }
            } else {
                foreach($submission->errors() as $error){
                    echo $error, '<br>';
                }
            }
        }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Submit Application</title>
    </head>
    <body>
        <form action="" method="post">
            <table>
                <tr>
                    <td>email :</td>
                    <td><input type="text" name="email" value=""></td>
                </tr>
                <tr>
                    <td>ID Number :</td>
                    <td><input type="text" name="id_number" value=""></td>
                </tr>
                <tr>
                    <td>Position Applied :</td>
                    <td>
                        <select name="positions">
                            <option>Non Executive</option>
                            <option>Executive</option>
                            <option>Management</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" name="Submit" value="Submit Application"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>
    

    Sender.php

    <?php
        class Check{
            private $_valid = false,
                    $_errors = array(),
                    $_db,
                    $_count  = 0;
            public function __construct(){
                $this->_db = // Connection to DB using PDO
            }
            public function filterForm($source, $items = array()){
                foreach($items as $item => $rules){
                    foreach($rules as $rule => $rule_value){
    
                        $inputValue = $source[$item];
                        if($rule === 'required' && empty($inputValue)){
                            $this->addError("{$item} is required");
                        } else if(!empty($inputValue)){
                            switch($rule){
                                case 'unik':
                                    $checkUnik = $this->_db->get($rule_value, array($item, '=', $value));
                                    if($checkUnik->count()){
                                        $this->displayError("{$item} already exists");
                                    }
                                break;
                                case 'duplicate':
                                    $checkDuplicate = $this->_db->get($rule_value, array($item, '=', $value));
                                    if($checkDuplicate->count()){
                                        $checkUsers = $this->_db->query("SELECT * FROM resumes");
                                        if($checkUsers->count()){
                                            $this->displayError("User already apply this positions");
                                        }
                                    }
                                break;
                            }
                        }
                    }
                }
                if(empty($this->_errors)){
                    $this->_passed = true;
                }
                return $this;
            }
    
            public function valid(){
                return $this->_valid;
            }
            public function errors(){
                return $this->_errors;
            }
            public function displayError($error){
                $this->_errors[] = $error;
            }
            public function count(){
                return $this->_count;
            }
        }
    ?>
    

1 个答案:

答案 0 :(得分:0)

如果我理解您的问题,user_id不应该是AUTO_INCREMENT。而是创建一个id列作为主键并且是AUTO_INCREMENT:)

这也值得通过http://www.phptherightway.com/阅读:)