Raza Barlas Mills
Raza Barlas Mills

Reputation: 19

Problem in duplication after clicking submit button multiple times

My main problem is with that code in which when I click on submit buttons many times, it inserts duplication many times in the database in which I need to avoid that. Please help me to solve this problem. These are the two tables in which I am trying to insert. mat_ans_options_choose and mat_answer.

$val                 = $this->input->post(null, true);
$val['id']           = $this->input->post('id');
$val['sub_type']     = $this->input->post('sub_type');
$val['timeout']      = $this->input->post('timeout');
$val['level']        = $this->input->post('level');
$val['mat_category'] = $this->input->post('mat_category');
$option              = $val['option']              = $this->input->post('option');
$type                = $this->input->post('type');
$marks               = [];
$uid                 = $this->session->userdata('id');
if (isset($val['id']) && isset($option)) {
    $query  = $this->db->query("SELECT * FROM mat_ans_options WHERE deleted=0 AND active=1 AND question=" . $val['id']);
    $result = $query->result_array();
    if ($query->num_rows() > 0) {
        $count1 = 1;
        foreach ($result as $res) {
            if ($res['marks'] == 1) {
                break;
            } else {
                $count1++;
            }
        }
    }
    // MAT answers options choose
    $query1  = $this->db->query("SELECT * FROM mat_ans_options_choose WHERE deleted=0 AND active=1 AND uid=$uid AND q=" . $val['id']);
    $result1 = $query1->result_array();
    if ($query1->num_rows() > 0) {} else {
        $data1 = [
            'uid'          => $uid,
            'q'            => $val['id'],
            'option_chose' => $option,
            'createdon'    => $this->general_model->server_time(),
        ];
        $this->db->insert('mat_ans_options_choose', $data1);
    }
    if ($count1 == $option) {
        $marks = 1;
    } else {
        $marks = 0;
    }
    //   if($marks==1 || $marks==0)
    //  {
    // MAT answers
    $query2  = $this->db->query("SELECT * FROM mat_answers WHERE deleted=0 AND active=1 AND uid=$uid AND q=" . $val['id'] . " AND type=" . $type . " AND sub_type=" . $val['sub_type'] . " AND level=" . $val['level']);
    $result2 = $query2->result_array();
    if ($query2->num_rows() > 0) {} else {
        $data = [
            'uid'           => $uid,
            'q'             => $val['id'],
            'type'          => $type,
            'level'         => $val['level'],
            'sub_type'      => $val['sub_type'],
            'mat_category'  => $val['mat_category'],
            'marks'         => $marks,
            'timeoutstatus' => $val['timeout'],
            'createdon'     => $this->general_model->server_time(),
        ];
        $this->db->insert('mat_answers', $data);
    }
    //    }
    return 1;
} else {
    return 0;
}

Upvotes: 0

Views: 461

Answers (3)

Yasir Mushtaq
Yasir Mushtaq

Reputation: 178

Create UNIQUE index in database for uid and q. The database will not insert same question's id from same user's id mulitple times.

Upvotes: 0

user5446912
user5446912

Reputation:

You can use JS/jQuery to limit the number of requests made on the client side. For example by disabling the button on submit:

$("#my-button").prop("disabled", true);

But if the data is sensitive for duplicates (orders, user registration etc) you should make the request limit server side with PHP. You can achieve this by adding a unique index to the tables, either on user id or on a unique token that is submitted with the html form.

Upvotes: 0

Grzegorz Lasak
Grzegorz Lasak

Reputation: 96

Use JS in which you disable the button after first click - it will work no matter if you are using AJAX or not.

Upvotes: 1

Related Questions