PHP OOP - writing duplicate records
I have tables in my mysql database that resumes
I am using PHP OOP. I have this register.php form that the user has to fill out.
My goal:
- The same user can submit multiple attachments if items are not submitted twice. For example, "Thomas sends the first app using thomas@mail.com , id 1111 and is positioned as an executive , but he cannot send the same items to the second using the same email or id number."
Can anyone help? Below is my code. * Note. I have a problem with Check.php . I'm not sure how to check the area a 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.');
}
}
}
+3
Rozaimi
source
to share
1 answer
If I understand your problem correctly, user_id
there shouldn't be AUTO_INCREMENT
. Instead, make a column id
that is the primary key, and AUTO_INCREMENT
:)
Also it is really worth reading via http://www.phptherightway.com/ :)
0
martindilling
source
to share