javascript - Jquery Ajax Shows error 500 when ever sql returns error -
i working on codeigniter , getting problem while inserting values in database via ajax. problem occur when sql query throws error. controller code:-
public function register_data() { $data = array( 'user_name'=>$this->input->post('name'), 'user_email'=>$this->input->post('emailid'), 'user_pwd'=>$this->input->post('pwd'), 'user_pno'=>$this->input->post('pno'), 'user_gender'=>$this->input->post('gender'), 'user_age'=>$this->input->post('age'), 'dor'=>date('y-m-d h:i:s'),); $this->load->model('register'); $this->register->insert_content($data); $output = array(); //creating output array json response ajax $output['result'] = $this->register->getstatus(); echo $output['result'].'---'; $output['message'] = $this->register->getmessage(); echo $output['message']; echo json_encode($output); }
my model code :-
class register extends ci_model{ var $message; var $status; public function __construct() { parent::__construct(); $this->load->database(); } public function insert_content($da) { if ($this->db->insert('user',$da))//checking if mysql query successful { $this->status = true; $this->message = "data inserted"; } else { $this->status = false; $this->message = $this->db->error_message(); } } public function getstatus() { return $this->status; } public function getmessage() { return $this->message; }}
and ajax call:-
$.ajax({ url: "<?php echo site_url('home_controller/register_data');?>", data: $("#frm").serialize(), type: 'post', success: function (data) { alert(data); var resp = $.parsejson(data); if (resp.result) { window.location.href = "<?php echo site_url('home_controller');?>"; } else { $("#msgs").html(resp.message); } }, error: function (error) { $("#msgs").html(json.stringify(error)); //this returned when model condition inserting data throws error. } });
and please note datas getting inserted. hence there no problem while inserting. have used json return response ajax. problem occurs when inserting goes wrong. example:- have kept email unique in database hence when insert duplicate in db mysql throws error says duplication error, u can see code in model have made insert code in if condition , when insert query gets failed status , msg stored in variable. how used in core php. have no idea how handle in codeigniter. kindly provide solutions. appreciated.
codeigniter has included error() function display db error. db->error_message() problem hear
Comments
Post a Comment