-1
我想檢查插入unit_id時,我們必須檢查unit_id alraedy用於另一個艦隊或沒有。當已經使用顯示消息錯誤並且未使用另一個車隊時,unit_id可以插入到數據庫中。無法插入數據庫codeigniter
這是控制器
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|callback_unit_check');
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 jumlah 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 as status, fleet_member.unit_id as unit
from fleet, fleet_member
where fleet_member.unit_id = '$unit_id' AND fleet.id_fleet = '$fleet'");
$ans2 = $query->row();
if ($ans->jumlah > 0)
{
$this->session->set_flashdata('message', generateErrorMessage('Data Fleet ID Sudah Digunakan'));
redirect(site_url('fleet_member'));
}
else
{
if ($ans2->unit == $unit_id && $ans2->status == $fleet) {
$this->session->set_flashdata('message', generateErrorMessage('Data Unit ID Sudah Digunakan'));
redirect(site_url('fleet_member'));
}
else
{
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'));
}
}
}
我得到的錯誤與此代碼的代碼。
當我插入unit_id相同unit_id在另一個車隊數據不能插入到數據庫和顯示按摩'數據單元ID Sudah Digunakan'。 (它的權利)
當我插入unit_id與另一個unit_id在anohter艦隊數據不能插入到數據庫和顯示按摩'數據單元ID Sudah Digunakan'。 (它錯了),因爲它必須插入數據庫。
你能幫我解決這個問題嗎?