I have to tables in my mysql database which is resumes
I'm using PHP OOP And I have this form register.php that user need to fill in.
My goal is to :
Can anyone help ? Below is my code. *Note : I'm having a problem on Check.php. Im not sure how to check in case 'duplicate': area.
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.');
}
}
}
If I understand your problem correctly, user_id
should not be AUTO_INCREMENT
. Instead make an id
column that is the primary key and is AUTO_INCREMENT
:)
Also this is really worth reading through http://www.phptherightway.com/ :)