CodeIgniter Forums
difficulty with insert_batch - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: difficulty with insert_batch (/thread-70883.html)

Pages: 1 2


difficulty with insert_batch - n0cturn0 - 06-13-2018

hi guys..

I'm having a hard time, to insert the array, with the format



Array
(
   [nraluno] => 3745
   [nomeguerra] => ARTHUR COMPANS
   [nrmat] => 36422
   [serie] => 6º
   [trimestre] => 2
   [turma] => 601
   [nrano] => 49
   [status] => 1
   [disciplinas] => Array
       (
           [0] => Ciências Físicas e Biológicas
       )

   [nrbase] => Array
       (
           [0] => 1087
       )

)

Array
(
   [nraluno] => 3941
   [nomeguerra] => CALHEIROS
   [nrmat] => 36678
   [serie] => 6º
   [trimestre] => 2
   [turma] => 601
   [nrano] => 49
   [status] => 1
   [disciplinas] => Array
       (
           [0] => História
           [1] => LEM - Inglês
       )

   [nrbase] => Array
       (
           [0] => 1089
           [1] => 1092
       )

)
 usage for insert:
foreach ($alunos as $key => $value) {
                    $saida_aluno[] = array (
                    

                        
                        'disciplinas'        => $value['disciplinas'],
                        
                        'nrbase'            => $value['nrbase'],
                        'nraluno'             => $value['nraluno'],
                        'nomeguerra'        => $value['nomeguerra'],
                        'nrmat'             => $value['nrmat'],
                        'serie'                => $value['serie'],
                        'trimestre'            => $value['trimestre'],
                        'turma'                => $value['turma'],
                        'nrano'                => $value['nrano'],
                        'status'            => $value['status'],     //1 ENTRADA 0  SAIDA
                        'data_geracao'     => $data_controle,
                        'nrcomunicado'     => $last,
                        'nrcomunicado'     => $last_insert
                            );
                        
                    $this->db->insert_batch('cmcg_controleaps', $saida_aluno);
                }


an return 

Error Number:
ERROR: syntax error at or near "," LINE 1: ..., "trimestre", "turma") VALUES ('2018-06-13',Array,'ARTHUR C... ^
INSERT INTO "cmcg_controleaps" ("data_geracao", "disciplinas", "nomeguerra", "nraluno", "nrano", "nrbase", "nrcomunicado", "nrmat", "serie", "status", "trimestre", "turma") VALUES ('2018-06-13',Array,'ARTHUR COMPANS','3745',49,Array,95,'36422','6º',1,2,'601')

Confused


RE: difficulty with insert_batch - php_rocs - 06-13-2018

@n0cturn0,

Questions...
What version of CI are you running?
Have you tried running the query using a mysql tool (that connects directly to the database)?
Are you sure that your values match up with your table columns correctly?


RE: difficulty with insert_batch - dave friend - 06-13-2018

Probably won't fix the SQL syntax error, but you should move the insert_batch() call outside the foreach() loop.

PHP Code:
foreach ($alunos as $key => $value)
{
    
$saida_aluno[] = array(
        
'disciplinas' => $value['disciplinas'],
        
'nrbase' => $value['nrbase'],
        
'nraluno' => $value['nraluno'],
        
'nomeguerra' => $value['nomeguerra'],
        
'nrmat' => $value['nrmat'],
        
'serie' => $value['serie'],
        
'trimestre' => $value['trimestre'],
        
'turma' => $value['turma'],
        
'nrano' => $value['nrano'],
        
'status' => $value['status'], //1 ENTRADA 0  SAIDA
        
'data_geracao' => $data_controle,
        
'nrcomunicado' => $last,
        
'nrcomunicado' => $last_insert);
}
$this->db->insert_batch('cmcg_controleaps'$saida_aluno); 



RE: difficulty with insert_batch - n0cturn0 - 06-13-2018

Not success, only now inserting the first value the 'trimestre' array:

the code changed..
Tanks guys!!


foreach ($alunos as $key => $value) {
                    
                    foreach ($value['disciplinas'] as $k => $v) {
                        $array = array();
                        $array = $v;
                    }
                    $saida_aluno[] = array (
                        'disciplinas'        => $array,
                        
                        //'nrbase'            => $value['nrbase'],
                        'nraluno'             => $value['nraluno'],
                        'nomeguerra'        => $value['nomeguerra'],
                        'nrmat'             => $value['nrmat'],
                        'serie'                => $value['serie'],
                        'trimestre'            => $value['trimestre'],
                        'turma'                => $value['turma'],
                        'nrano'                => $value['nrano'],
                        'status'            => $value['status'],     //1 ENTRADA 0  SAIDA
                        'data_geracao'     => $data_controle,
                        'nrcomunicado'     => $last_insert
                            );
                        
                
                }
                $this->db->insert_batch('cmcg_controleaps', $saida_aluno);


RE: difficulty with insert_batch - neuron - 06-14-2018

If look user to error message you can see that one of values you set is array:
Error Number:
ERROR: syntax error at or near "," LINE 1: ..., "trimestre", "turma") VALUES ('2018-06-13',Array,'ARTHUR C... ^
INSERT INTO "cmcg_controleaps" ("data_geracao", "disciplinas", "nomeguerra", "nraluno", "nrano", "nrbase", "nrcomunicado", "nrmat", "serie", "status", "trimestre", "turma") VALUES ('2018-06-13',Array,'ARTHUR COMPANS','3745',49,Array,95,'36422','6º',1,2,'601')


RE: difficulty with insert_batch - n0cturn0 - 06-14-2018

(06-14-2018, 02:28 AM)neuron Wrote: If look user to error message you can see that one of values you set is array:
Error Number:
ERROR: syntax error at or near "," LINE 1: ..., "trimestre", "turma") VALUES ('2018-06-13',Array,'ARTHUR C... ^
INSERT INTO "cmcg_controleaps" ("data_geracao", "disciplinas", "nomeguerra", "nraluno", "nrano", "nrbase", "nrcomunicado", "nrmat", "serie", "status", "trimestre", "turma") VALUES ('2018-06-13',Array,'ARTHUR COMPANS','3745',49,Array,95,'36422','6º',1,2,'601')

perfectly!!! I can not insert this value array disciplinas..


RE: difficulty with insert_batch - php_rocs - 06-14-2018

@n0cturn0,

You should see what value is in $array prior to you creating your batch query. Your issue might be in the code below

foreach ($value['disciplinas'] as $k => $v) {
$array = array();
$array = $v;
}

What value is suppose to be kicked out of the foreach loop when it completes?


RE: difficulty with insert_batch - n0cturn0 - 06-14-2018

(06-14-2018, 08:33 AM)php_rocs Wrote: @n0cturn0,

You should see what value is in $array prior to you creating your batch query.  Your issue might be in the code below

foreach ($value['disciplinas'] as $k => $v) {
                       $array = array();
                       $array = $v;
                   }

What value is suppose to be kicked out of the foreach loop when it completes?

 tanks to reply!
I need all the values the disciplinas array!!


RE: difficulty with insert_batch - php_rocs - 06-14-2018

@n0cturn0

While you may need all the values in the array the field in the database may be expecting a string value instead of an array object. This is why I recommend that you see what value the foreach loop is kicking out (which I suspect is an array object).


RE: difficulty with insert_batch - n0cturn0 - 06-14-2018

[email protected]

While you may need all the values in the array the field in the database may be expecting a string value instead of an array object.  This is why I recommend that you see what value the foreach loop is kicking out (which I suspect is an array object).

  I understood that field in the database may be expecting a string value instead of an array object.
  Do not know, to do the loop 'disciplinas array ' for insert field the db.

example:
Array
(
 [
   [nraluno] => 3941
   [nomeguerra] => CALHEIROS
   [nrmat] => 36678
   [serie] => 6º
   [trimestre] => 2
   [turma] => 601
   [nrano] => 49
   [status] => 1
   [disciplinas] => Array
       (
           [0] => História
           [1] => LEM - Inglês
       )

   [nrbase] => Array
       (
           [0] => 1089
           [1] => 1092
       )

)

insert two value  the array disciplinas in field db, and the rest the array (nraluno,nomedeguerra,nrmat..) repeat at the insert.
sorry my english!