投票系统候选人[关闭]

I want to create a vote system to choose a new head village. How can I make a function to vote the candidate ?

Each user can vote only once.

My code (I know this will be wrong):

function vote_calon(){

    $id_calon = $this->input->post('id_calon');
    $voted= $this->input->post('voted');
    $data = array(
        'voted' =>  'set ++1',
    );

    $where = array(
        'id_calon' => $id_calon,        
    );

    $this->Admin_dcalon->vote_calon($where,$data,'calon');      
    redirect('voter/voter_sukses');
}

First, I guess you have already logged user or submitted user, not guest/anonymous which not login yet or not submit input of his name. Otherwise the code workflow will be different.

Table in your database:

table
#calon_table => profile of governor or person who will voted 
id [int auto_increment]
name [varchar]

#user_table = profile of user who will vote
id [int auto_increment]
name [varchar]
password, avatar, and another information here

#vote_table = table to store vote result
id_user [int ,PRIMARY KEY, foreign key of user_table.id] //while this set as Primary Key , you are never get duplicate user for voting
id_calon [int ,foreign key of calon_table.id]

And the code will be:

function vote_calon(){
    $id_calon = $this->input->post('id_calon'); //example : 2
    $id_user = $this->input->post('voted'); //example: 324234
    $data = array(
        'id_user' =>  $id_user,
        'id_calon' =>  $id_calon
    );

    $where = array(
        'id_user' => $id_user //this is where clause to keep 1 user only can vote 1 governor
    );

    $check = $this->db->where('vote_table', $where);
    if($check->num_rows() > 0)
    {
        echo "you already use your vote"; //this is for make sure user cant vote more than 1
        //Or you can try update: $this->db->update('vote_table', $data, $where);
    }
    else
    {
        //insert data
        $this->db->insert('vote_table', $data);
    }

    redirect('voter/voter_sukses');
}