Insert in the database according to the data found

0

I have a certain problem in "logic", where I should insert the records of two arrays into a table of the DB. Here I have the following situation:

At first if I check that tabela_p does not contain data. If it does not contain, it adds null to tabela_p and inserts according to how much data it has tabela_s ;

if(count($tabela_p) == 0){
    $tabela_p = null;
    for( $i = 0; $i < count( $tabela_s); $i ++ ) {

        $w_querybusca="insert into sai_cad_patr_seri (fk_seq_cara_peri,tx_num_patr,tx_num_seri)
                  values ('$arr_w_param[17]','$tabela_p[$i]','$tabela_s[$i]');"; 

        $w_queryresultado = f_class_conecta_bd($w_querybusca);          
    }
}
else
{
    for( $i = 0; $i < count($tabela_p); $i ++ ) {
        $w_querybusca="insert into sai_cad_patr_seri (fk_seq_cara_peri,tx_num_patr,tx_num_seri)
                  values ('$arr_w_param[17]','$tabela_p[$i]','$tabela_s[$i]');";                       
        $w_queryresultado = f_class_conecta_bd($w_querybusca);          
    }       
}

Soon in else I enter as tabela_p ; The problem is:

  • If it has a value in tabela_p it falls into else and will execute it only once. But it has the possibility to contain 2 or more data in tabela_s ai it will not insert them.

How should I do to fix this logic?

    
asked by anonymous 30.10.2014 / 12:55

2 answers

1

You use p as a parameter of for . Use s , since you want s to set the loop.

Switch

for( $i = 0; $i < count($tabela_p); $i ++ ) 

by

for( $i = 0; $i < count($tabela_s); $i ++ )
    
30.10.2014 / 13:01
0

You can do 2 something like:

If you are always sure that there will be a value in the $ table_s variable, then only foreach is sufficient.

foreach ($tabela_s as $key_s => $value_s){
        foreach ($tabela_p as $key_s=> $value_p){
            $w_querybusca="insert into sai_cad_patr_seri (fk_seq_cara_peri,tx_num_patr,tx_num_seri) values ('$arr_w_param[17]',$value_p,'$value_s;"; 
            $w_queryresultado = f_class_conecta_bd($w_querybusca);          
        }
    }
    
30.10.2014 / 14:11