3
我要在我的MySQL数据库中的表是恢复 PHP OOP - 重复的记录条目
我使用PHP OOP和我有这种形式register.php用户需要填写
我的目标是:
- 同一用户只要级位置d提交多个申请oes不发送两次。例如,“托马斯使用[email protected],ID 和位置执行发送第一应用但使用相同的电子邮件地址或身份证号码,他不能提交第二次相同的位置“
任何人都可以帮忙吗?以下是我的代码。 *注意:我在Check.php有问题。我不知道如何检查案例'重复':区域。
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.');
}
}
}
我建议增加对,是独特的领域addition'复合键'。也就是说,将两个字段组合在一起并且设置为唯一的密钥。这可以防止代码中的错误创建重复项,并使重复项无法进行。您仍然需要正确的代码来隐藏最终用户的数据库错误,但这是正确的做法......海事组织。让应用程序执行它是很好的,但是在数据库上强制执行唯一性是失败点或最后一道防线。 – ArtisticPhoenix
代码不应该进入7级深度。你做错了什么。 – user2914191