Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 3 is not inserting the data
#1

(This post was last modified: 08-29-2019, 06:28 AM by ciadmin.)

I am creating the post  via angular 8, but codeigniter is not inserting  the data, please have a look of the code, that I have done.

Code:
public function  add_bankaccount()
    {

            header("Access-Control-Allow-Origin: *");
            header("Access-Control-Request-Headers: GET,POST,OPTIONS,DELETE,PUT");
            header('Access-Control-Allow-Headers: Accept,Accept-Language,Content-Language,Content-Type');
            $response= [];
            $status=0;
            $error_mess='Some error occured.';
            $success_mess='';

            $formdata = json_decode(file_get_contents('php://input'),true);

            $name=$formdata['name'];
            $bank_name=$formdata['bank_name'];
            $iban=$formdata['iban'];
            $user_id=$formdata['user_id'];
           

            $is_account_exist=get_table_data('id', 'tbl_bank', ['user_id'=>$user_id,'iban'=>$iban]);
           
            if(!empty($is_account_exist))
            {
                $status=2;
                $error_mess='Account already exist.';

            }
            else
            {

                $save_data= [];

                $save_data['name']= $name;
                $save_data['bank_name']= $bank_name;
                $save_data['iban']= $iban;
                $save_data['user_id']= $user_id;
                $save_data['status']= 1;
                $save_data['date_added']= date('Y-m-d H:i:s');
                $save_data['date_modified']= date('Y-m-d H:i:s');

                // print_r($save_data); exit;

                 

                  $this->db->insert('tbl_bank',$save_data );

                  $status=1;
                  $error_mess='';
                  $success_mess='Account added successfully.';


            }





           

            //pr($formdata); exit;


            $response['status']=$status;
            $response['error_mess']=$error_mess;
            $response['success_mess']=$success_mess;

            $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode($response));
   
    }

ERROR - 2019-08-29 15:10:57 --> Query error: Column 'name' cannot be null - Invalid query: INSERT INTO `tbl_bank` (`name`, `bank_name`, `iban`, `user_id`, `status`, `date_added`, `date_modified`) VALUES (NULL, NULL, NULL, NULL, 1, '2019-08-29 15:10:57', '2019-08-29 15:10:57')


Please suggest the solution for above.

EDIT: moderator added [ code ] tags for readability!!

Attached Files Thumbnail(s)
   
Reply
#2

@asheesharyan,

Did you check your table settings for the 'name' field? What is it?
Reply
#3

(08-29-2019, 07:28 AM)php_rocs Wrote: @asheesharyan,

Did you check your table settings for the 'name' field?  What is it?

It has varchar, also when I print the save_data,it is printing all the data, but after the execution of the query , it convert all the post data to the null...
Reply
#4

@asheesharyan,

What is the value of $formdata prior to assigning the values to variables? Also, what is the value of $is_account_exist prior to determining if the account exist?
Reply
#5

(08-30-2019, 08:49 AM)php_rocs Wrote: @asheesharyan,

What is the value of $formdata prior to assigning the values to variables? Also, what is the value of $is_account_exist prior to determining if the account exist?

$formdata, printing all the post data like bank_name, iban and user_id etc,  $is_account_exist, returns the array.
Reply
#6

@asheesharyan,

Try this instead. Use your $formdata values to set your $savedata values...

$save_data['name']= $formdata['name'];
$save_data['bank_name']= $formdata['bank_name'];
$save_data['iban']= $formdata['iban'];
$save_data['user_id']= $formdata['user_id'];

If you are still receiving the same errors then there is something wrong with the $formdata.
Reply
#7

$save_data= [];
                $save_data['name']=$name;
                $save_data['bank_name']= $formdata['bank_name'];
                $save_data['iban']=$formdata['iban'];
                $save_data['user_id']=$formdata['user_id'];

                $save_data['status']= 1;
                $save_data['date_added']= date('Y-m-d H:iConfused');
                $save_data['date_modified']= date('Y-m-d H:iConfused');
               

                echo $sql = $this->db->set($save_data)->get_compiled_insert('tbl_bank');
               

                $this->db->query($sql);


              Output: Please find the attachment

Attached Files Thumbnail(s)
   
Reply
#8

@asheesharyan,

Is it possible to var_dump (dump to the screen) what is in $formdata (I need to see what is in there). The values must be NULL.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB