I'd like to know how to prevent an insert into a database when the data is already existing inside it. And how to make an onclick window with a message that alerts the user that the data they're inserting is already inside the database.
How I get inputs from my view goes a little bit like this:
<form method="post" action="<?php echo base_url();?>index.php/Controller/insertdata">
<h3>Select Course Code:</h3>
<select id="employeeid" name="empid">
<option value="" selected="selected">---Select Course Code---</option>
<?php foreach ($e_id as $row4): ?>
<option label="<?php echo $row4['EmpID']; ?>" value="<?php echo $row4['EmpID']; ?>" <?php echo set_select('course', $row4['EmpID'], False); ?>> <?php echo $row4['EmpID'] ; ?> </option>
<?php endforeach; ?>
</select>
<input type="Submit" value="Submit" id="submite_id">
It takes data from the database into the select tag. My database has Employee ID, Employee name, Dept. code, and assignment.
Here's my Controller:
public function insertdata() {
$this->model->setdata();
$data['results'] = $this->model->emp_all();
$this->load->view('edit_view', $data);
}
It inserts the data into the database and redirects the user to a view.
And finally here's how I insert it into the database in my Model:
public function setData(){
$f1 = $_POST['empid'];
$f2 = $_POST['empname'];
$f3 = $_POST['deptcode'];
$f4 = $_POST['assignment'];
$this->db->query("INSERT INTO employeeDB VALUES('$f1', '$f2', '$f3', '$f4')");
}
The catch is that the users should not be able to assign an employee id, and name to a duplicate assignment on the same department.
Any reply, comment, and insight will be appreciated. Thanks in advance!