无法在codeigniter中将数据插入数据库

时间:2014-12-22 08:50:44

标签: php mysql codeigniter if-statement insert

我有这个程序无法插入数据但总是重定向到“其他”并显示消息错误“数据单元ID Sudah Digunakan”

这是我的控制器

$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 (!($check == FALSE)) {

                    $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'));
                }

            }

这个程序可以插入当单位ID不在同一个文件中使用时。

你能帮忙解决这个程序中的插入数据吗?

1 个答案:

答案 0 :(得分:0)

使用需要使用

if(!($check)

或者您需要使用

if($check == FALSE)

因为我可以看到你在$ check变量中从数据库获取数据所以最好像这样做

 if($check->num_rows()==0)

我希望这能解决您的问题

此致

Imran Qasim