Welcome Guest, Not a member yet? Register   Sign In
difficulty with insert_batch
#1
Sad 

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
Reply
#2

@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?
Reply
#3

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); 
Reply
#4

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);
Reply
#5

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')
Reply
#6

(06-14-2018, 01: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..
Reply
#7

@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?
Reply
#8

(06-14-2018, 07: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!!
Reply
#9

(This post was last modified: 06-14-2018, 10:14 AM by php_rocs.)

@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).
Reply
#10

php_rocs@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).

  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!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB