如何在插入数据库之前验证重复条目 - Codeigniter

时间:2012-11-11 12:52:44

标签: codeigniter

我已经开发了简单的应用程序,我已经从数据库动态生成了网格中的复选框,但我的问题是当用户从网格中选中复选框和其他必填字段并按下提交按钮时,它会添加重复值,所以我想知道如何我可以查看复选框值&在向数据库提交数据时,具有数据库值的其他字段值。

以下代码用于生成所有选定的项目,然后保存太多db

    foreach ($this->addattendee->results as $key=>$value)
{
//print_r($value);
$id = $this->Attendee_model->save($value);
}

我正在使用codeigniter ....任何人都可以使用示例代码plz

 {
    $person = $this->Person_model->get_by_id($id)->row();
    $this->form_data->id = $person->tab_classid;
    $this->form_data->classtitle = $person->tab_classtitle;
    $this->form_data->classdate = $person->tab_classtime;
    $this->form_data->createddate = $person->tab_crtdate;
    $this->form_data->peremail = $person->tab_pemail;
    $this->form_data->duration = $person->tab_classduration;

    //Show User Grid - Attendee>>>>>>>>>>>>>>>>>>>>>>>>
    $uri_segment = 0;
    $offset = $this->uri->segment($uri_segment);
    $users = $this->User_model->get_paged_list($this->limit, $offset)->result();
    // generate pagination
    $this->load->library('pagination');
    $config['base_url'] = site_url('person/index/');
    $config['total_rows'] = $this->User_model->count_all();
    $config['per_page'] = $this->limit;
    $config['uri_segment'] = $uri_segment;
    $this->pagination->initialize($config);
    $data['pagination'] = $this->pagination->create_links();
    // generate table data
    $this->load->library('table');
    $this->table->set_empty(" ");
    $this->table->set_heading('Check', 'User Id','User Name', 'Email', 'Language');
    $i = 0 + $offset;
    foreach ($users as $user)
    {
        $checkarray=array('name'=>'chkclsid[]','id'=>'chkclsid','value'=>$user->user_id);
        $this->table->add_row(form_checkbox($checkarray), $user->user_id, $user->user_name, $user->user_email,$user->user_language
        /*,anchor('person/view/'.$user->user_id,'view',array('class'=>'view')).' '.
        anchor('person/update/'.$user->user_id,'update',array('class'=>'update')).' '.
        anchor('person/showattendee/'.$user->user_id,'Attendee',array('class'=>'attendee')).' '.
        anchor('person/delete/'.$user->user_id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this person?')"))*/ );
    }
    $data['table'] = $this->table->generate();

//结束网格代码

    // load view
    // set common properties
    $data['title'] = 'Assign Attendees';
    $msg = '';
    $data['message'] = $msg;
    $data['action'] = site_url('person/CreateAttendees');
    //$data['value'] = "sssssssssssssssssss";
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = "<p>Welcome:"." ".$session_data['username']. " | " . anchor('home/logout', 'Logout')." | ". "Userid :"." ".$session_data['id']; "</p>";
    $data['link_back'] = anchor('person/index/','Back to list of Classes',array('class'=>'back'));
    $this->load->view('common/header',$data);
    $this->load->view('adminmenu');
    $this->load->view('addattendee_v', $data);

}

2 个答案:

答案 0 :(得分:0)

代码非常混乱,但我在我的应用程序中解决了类似的问题,我认为,我不确定它是否是最好的方法,但它确实有效。

function save_vote($vote,$show_id, $stats){
    // Check if new vote
    $this->db->from('show_ratings')
            ->where('user_id', $user_id)
            ->where('show_id', $show_id);
    $rs = $this->db->get();
    $user_vote = $rs->row_array();
            // Here we are check if that entry exists
    if ($rs->num_rows() == '0' ){
        // Its a new vote so insert data

        $this->db->insert('show_ratings', $rate);

    }else{
        // Its a not new vote, so we update the DB. I also added a UNIQUE KEY to my database for the user_id and show_id fields in the show_ratings table. So There is that extra protection.
        $this->db->query('INSERT INTO `show_ratings`  (`user_id`,`show_id`,`score`) VALUES (?,?,?) ON DUPLICATE KEY UPDATE `score`=?;', array($user_id, $show_id, $vote, $vote));
        return $update;
    }
}

我希望这段代码能让您知道该怎么做。

答案 1 :(得分:0)

也许我和你有同样的麻烦。 这就是我所做的。

<?php
public function set_news(){
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $query = $this->db->query("select slug from news where slug like '%$slug%'");
        if($query->num_rows()>=1){
            $jum = $query->num_rows() + 1;
            $slug = $slug.'-'.$jum;
        }

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text')
        );

        return $this->db->insert('news', $data);        
    }
?>

然后它有效。