CodeIgniter Forums
codeigniter ajax database error handling - Printable Version

+- CodeIgniter Forums (
+-- Forum: Development (
+--- Forum: CodeIgniter 3.x (
+--- Thread: codeigniter ajax database error handling (/showthread.php?tid=79350)

codeigniter ajax database error handling - jackey - 06-01-2021

When I try to add duplicate entry, it give error like this.
I want to the error "Duplicate entry '[email protected]' for key 'email' " to be visible in my view page inside div tag as alert-danger
Can any one pls help me 

<form id="form_temp_student" name="form_temp_student" action="#" >
            <table class="table align-middle table-bordered table-striped">                                     

                                <td>Full Name </td>
                                <td colspan="3">  <div class="form-control"><input type="text" name="full_name" id="full_name" style="width:900px" required><span class="help-block"></span></div> </td>
                                <td> Initials</td>
                                <td colspan="3"> <div class="form-control"><input type="text" name="initials" id="initials" style="width:200px" required><span class="help-block"></span></div> </td>
                                <td> First Name</td>
                                <td> <div class="form-group"><input type="text" name="first_name" id="first_name" style="width:200px" required> <span class="help-block"></span></div></td>
                                <td>Last Name </td>
                                <td> <div class="form-group"><input type="text" name="last_name" id="last_name" style="width:300px" required> <span class="help-block"></span> </div></td>

                                <td> email</td>
                                <td colspan="3"> <input type="email" name="email2" id="email2" style="width:300px" required> </td>

          <button type="button" class="btn btn-default" id="btn_add_emp" onClick="add_temp_student()"> &nbsp;SUBMIT</button>
          <button type="button" class="btn btn-default" id="btn_add_cancel"> &nbsp;CANCEL</button>

PHP Code:
function add_temp_student()
var formData = new FormData($('#form_temp_student')[0]);
            url "<?php echo site_url('index.php/Login/add_temp_student')?>",      
: function(data)


            error: function (jqXHRtextStatuserrorThrown)



PHP Code:
  public function add_temp_student() {           
    $this->form_validation->set_rules('full_name','Full Name','trim|required');
    $this->form_validation->set_rules('first_name','First Name','trim|required');              
if ($this->form_validation->run() == false) {
            $response = array(
                'status' => FALSE,
                'message' => '<div class="alert alert-danger alert-dismissible"  role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true"></span></button>'.validation_errors().'</div>'

            echo json_encode($response);
    else {
                          $data = array( 
                          'full_name' => $this->input->post('full_name'),
                          'initials' => $this->input->post('initials'),
                          'first_name' => $this->input->post('first_name'),  
'last_name' => $this->input->post('last_name'),
                          'email'=> $this->input->post('email2')


                          $this->session->set_flashdata('message_ok''Your Data successfully added to the system. You will be contacted via email');
                          echo json_encode(array("status" => TRUE));
                  }else {
= array(
                      'status' => FALSE,
                      'message' => '<div class="alert alert-danger alert-dismissible"  role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true"></span></button>' .$this->db->error().'</div>'
                  echo json_encode($response2);



PHP Code:
public function model_add_temp_student($data){

  if ($succes_insert) {
            return true;
        } else {
->session->set_flashdata('message_error','<div class="alert alert-danger">  Please try again !</div>'.$this->db->error()); 

RE: codeigniter ajax database error handling - includebeer - 06-05-2021

You need to validate your data before you try to insert in the database. This way you catch the error and display it to the user. You need to do this to prevent the database from throwing an error.

I have a tutorial on how to use the validation class:

You just need to adjust what rule you need. In your case you will want the is_unique rule:

I just saw this was posted in the CI3 forum. There's also a validation class available in CI3, I just don't have any example on how to use it...