【问题标题】:PHP OOP - Duplicate records entryPHP OOP - 重复记录条目
【发布时间】:2018-01-15 10:19:40
【问题描述】:

我必须在我的 mysql 数据库中创建表 resumes

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

我的目标是:

  1. 同一用户可以提交多个申请,只要职位不发送两次即可。例如“Thomas 使用 thomas@mail.com 发送第一个应用程序,id 1111 和位置为 执行官,但他不能使用相同的电子邮件地址或身份证号第二次提交相同的职位”

谁能帮忙?下面是我的代码。 *注意:我在 Check.php 上遇到问题。我不确定如何检查 case 'duplicate': 区域。

Register.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>

Check.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;
        }
    }
?>

Sender.php

<?php
    class Sender{
        private $_db;
        public function __construct($user = null){
            $this->_db = // Connection to DB
        }
        public function create($fields = array()){
            if(!$this->_db->insert('resumes', $fields)){
                throw new Exception('You have a problem adding information.');
            }
        }
    }

【问题讨论】:

  • 我建议在那些唯一的字段上添加in addition 一个复合键。也就是说,采用两个字段的键将它们组合起来并设置为唯一的。这可以防止代码中的错误创建重复项,并使重复项成为不可能。您仍然需要正确的代码来隐藏最终用户的数据库错误,但这是正确的做法...... IMO。让您的应用程序这样做很好,但在数据库上强制执行唯一性是故障点,或最后一道防线。
  • 代码不应有 7 层深。你做错了什么。

标签: php mysql class oop switch-statement


【解决方案1】:

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

这也真的值得阅读http://www.phptherightway.com/:)

【讨论】:

    猜你喜欢
    • 2019-11-16
    • 2012-02-12
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多