So, I have form for my project where I can create users into database. There

are three types of user types, admins, moderators and end users.

My query for creating the users table is following:

CREATE TABLE IF NOT EXISTS users (

id SMALLINT(5) UNSIGNED PRIMARY KEY AUTO_INCREMENT,

name VARCHAR(70) NOT NULL,

email VARCHAR(70) NOT NULL,

username VARCHAR(9) NOT NULL,

password VARCHAR(60) NOT NULL,

user_type ENUM('admin','moderator','enduser'),

phone_number VARCHAR(10) NOT NULL,

school_id SMALLINT(5) UNSIGNED DEFAULT NULL,

CONSTRAINT fk_school_id

FOREIGN KEY (school_id)

REFERENCES schools(id)

ON UPADTE CASCADE ON DELETE RESTRICT,

subject_id SMALLINT(5) UNSIGNED DEFAULT NULL,

CONSTRAINT fk_subject_id

FOREIGN KEY (subject_id)

REFERENCES subjects(id)

ON UPDATE CASCADE ON DELETE RESTRICT

) ENGINE = INNODB;

So the school_id and subject_id are working as a foreign keys.

In my form, I have select menu where I can choose to create a certain type of user. If I choose to create admin, form will not display the hidden select menus for school and subject. If I choose to create moderator, form will display the hidden select menus. This is done with JavaScript.

Everything works well when I create an admin. It will store all the values in the database and sets school_id and subject_id values to NULL. Problem occurs when I decide to create a moderator. It still sets the school_id and subject_id values to even though I've set those values in my select menus.

I know I need to build a dynamic query. Just don't know how to implement it in the right way in CodeIgniter. If I was just coding with spaghetti PHP, it would be a peace of cake, but since I desperately want to learn using MVC frameworks, that is not an option.

Here's my model:

public function add_user($data) {

$data = array(

'username' => $data['username'],

'name' => $data['name'],

'email' => $data['email'],

'password' => $data['password'],

'user_type' => $data['user_type'],

'phone_number' => $data['phone_number']

);

$this->db->insert('users', $data);

}

Should I include those school_id and subject_id values in the array or not?