JavaScript Jquery Ajax Shows error 500 when ever sql returns error jquery ajax data shows object object

I am Working on Codeigniter and am getting a problem while inserting values in database via ajax. Problem occur when sql query throws an error. This is My 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 for json response to ajax
    $output['result'] = $this->Register->getStatus();    echo $output['result'].'---';
    $output['message'] = $this->Register->getMessage();  echo $output['message'];
    echo json_encode($output);
}

My Model code is so :-

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 is 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 this is My 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 is returned when model condition for inserting data throws error.
    }
});

Red Color text is an error which is displayed when sql duplication occurs

And please note that datas are getting inserted. Hence there is no problem while inserting. I have used json to return as response to ajax. Problem occurs only when inserting goes wrong. Example:- I have kept email as unique in database hence when i insert duplicate in db then mysql throws an error that says duplication error, as u can see my code in model that i have made insert code in if condition and that when insert query gets failed status and msg are stored in a variable. This is how i used to do in core php. I have no idea about how to handle this in codeigniter. Kindly provide a good solutions. Any Help Will be appreciated.

Answer:1

I need to store a list of points and check if a new point is already included in that list class Point { x: number; y: number; constructor(x: number, y: number) { this.x = x; ...

I need to store a list of points and check if a new point is already included in that list class Point { x: number; y: number; constructor(x: number, y: number) { this.x = x; ...

I'm using Auth0 rules to add some info to the user profile. When reading the profile from the web app: $userInfo = $auth0->getUser(); the data added by the rules does not seem to be there. Is ...

I'm using Auth0 rules to add some info to the user profile. When reading the profile from the web app: $userInfo = $auth0->getUser(); the data added by the rules does not seem to be there. Is ...

I've starting to dig my teeth into backbone and when working with a router function creating and destroying views I came across this little snippet, which didn't quite make sense to me as I've always ...

I've starting to dig my teeth into backbone and when working with a router function creating and destroying views I came across this little snippet, which didn't quite make sense to me as I've always ...

  1. javascript logical operator precedence
  2. javascript logical operator multiple conditions
  3. javascript logical operator short circuit
  4. javascript logical operator
  5. javascript logical operator return value
  6. javascript logical operator evaluation
  7. javascript logical operator priority
  8. javascript logical operator not
  9. javascript logical operators examples
  10. javascript logical operator xor
  11. javascript logical operator opposite
  12. javascript logical operator if statement
  13. javascript logical operators condition
  14. javascript logical operator check
  15. js logical operator
  16. js logical operator precedence
  17. javascript comparison operator
  18. javascript comparison operator as variable
  19. javascript comparison operator mean u003du003d
  20. javascript bool operator

var Greeter = (function () { function Greeter(message) { this.greeting = message; } Greeter.prototype.greet = function () { return "Hello, " + this.greeting; }; ...

var Greeter = (function () { function Greeter(message) { this.greeting = message; } Greeter.prototype.greet = function () { return "Hello, " + this.greeting; }; ...