CodeIgniter Forums
Insert array in database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Insert array in database (/showthread.php?tid=82911)



Insert array in database - pippuccio76 - 09-03-2022

HI i have this model :
Code:
protected $allowedFields = ['id','id_categoria_lavorazione','nome','codice','menu_linea_implantare','scala_colori','numero_dente','quantita_elementi','prezzo_vendita_da_file','gg_lavorativi','created_at','updated_at','deleted_at'];


scala_colori is type json in db

this is the $post variable from form:
Code:
Array ( [id_categoria_lavorazione] => 32 [nome] => test [codice] => DM4CCSX [menu_linea_implantare] => SI [scala_colori] => Array ( [0] => 37 [1] => 39 [2] => 19 [3] => 3 ) [numero_dente] => 10 [quantita_elementi] => 15 [prezzo_vendita_da_file] => 20 [gg_lavorativi] => 1 )

i simpli do 
this is my error : $descrizione_lavorazione_model->save($post);
Operand should contain 1 column(s)

How can i insert the array into json field ?

SOLVED:

Code:
            $post['scala_colori'] = json_encode($post['scala_colori']);


            $res=$descrizione_lavorazione_model->save($post);



RE: Insert array in database - InsiteFX - 09-04-2022

You should never put the $post or $get arrays into a database it's a security problem waiting to happen.


RE: Insert array in database - pippuccio76 - 09-04-2022

(09-04-2022, 12:46 AM)InsiteFX Wrote: You should never put the $post or $get arrays into a database it's a security problem waiting to happen.

Why?  However the insertion is on admin side .


RE: Insert array in database - InsiteFX - 09-05-2022

Because SQL code injection could be inserted into the $post or $get array and then executed.


RE: Insert array in database - pippuccio76 - 09-05-2022

(09-05-2022, 04:24 AM)InsiteFX Wrote: Because SQL code injection could be inserted into the $post or $get array and then executed.

Which is the correct way to insert array into json field of a db's table ?


RE: Insert array in database - InsiteFX - 09-06-2022

First check your $post or $get array to make sure it has the right data ( Never trust user input )

PHP How to INSERT a JSON object/array in a MySQLi Database


RE: Insert array in database - pippuccio76 - 09-06-2022

(09-06-2022, 12:20 AM)InsiteFX Wrote: First check your $post or $get array to make sure it has the right data ( Never trust user input )

PHP How to INSERT a JSON object/array in a MySQLi Database

i check first with validate...