i want to check when insert unit_id we must check unit_id alraedy used to another fleet or not. when already used show message error and not used another fleet unit_id can insert to database.
this is code in controller
function add_fleet_member()
{
$this->data['unit_list']= $this->munit_list->get_all_unit_list2();
$this->data['fleet'] = $this->mfleet->get_fleet();
$this->form_validation->set_rules('id_fleet', 'ID Fleet', 'required');
$this->form_validation->set_rules('unit_id', 'Unit', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->data['contents'] = $this->load->view('fleet_member',$this->data, true);
$this->load->view('template/wrapper',$this->data);
}
else
{
$fleet = $this->input->post('id_fleet');
$unit_id = $this->input->post('unit_id');
$records = array();
for ($i=0; $i < count($unit_id) ; $i++) {
$records[] = array(
'id_fleet' => $fleet,
'unit_id' => $unit_id[$i]
);
}
$query = $this->database_three->query("select
count(id_fleet_member) as unit from fleet_member
where id_fleet = '$fleet' AND unit_id = '$unit_id'
group by fleet_member.id_fleet");
$ans = $query->row();
$check = $this->database_three->query("
select fleet.fleet_status, fleet_member.unit_id
from fleet, fleet_member
where fleet_member.unit_id = '$unit_id' AND fleet.id_fleet = '$fleet'");
if ($ans->unit > 0)
{
$this->session->set_flashdata('message', generateErrorMessage('Data Fleet ID Sudah Digunakan'));
redirect(site_url('fleet_member'));
}
else
{
if ($check == NULL)
{
foreach ($records as $data) {
$query = "insert into fleet_member (id_fleet, unit_id) values ('".$data['id_fleet']."','".$data['unit_id']."')";
$this->database_three->query($query);
}
$this->session->set_flashdata('message', generateSuccessMessage('Data berhasil ditambah'));
redirect(site_url('fleet_member'));
}
else
{
$this->session->set_flashdata('message', generateErrorMessage('Data Unit ID Sudah Digunakan'));
redirect(site_url('fleet_member'));
}
}
}
}
i am getting error with this code.
when i insert unit_id with same unit_id in another fleet data cannot insert to database and show massage 'Data Unit ID Sudah Digunakan'. (its right)
when i insert unit_id with another unit_id in anohter fleet data cannot insert to database and show massage 'Data Unit ID Sudah Digunakan'. (its wrong) because it must insert to database.
can you help me solve this problem?
Aucun commentaire:
Enregistrer un commentaire