简体   繁体   中英

PHP does not insert in mySQL in foreach loop

I have a string array and I am trying to insert each data in this array into mySQL inside a foreach loop. But I recognized It doesn't insert all of them. Sometimes insert all, sometimes insert some of them. My code is below. Please let me figure out what my problem here is.

foreach($cbarray as $chvalue){
     list($kid, $odemeopt) = explode('-',$chvalue);
     $i_tr="trp".$kid;
     $i_en="enp".$kid;
     $i_fr="frp".$kid;

    $tr_kargop=doubleval($_POST[$i_tr]);
    $en_kargop=doubleval($_POST[$i_en]);
    $fr_kargop=doubleval($_POST[$i_fr]);                                                            

    if($odemeopt==1){
        $ik_tr="trk".$kid;
        $ik_en="enk".$kid;
        $ik_fr="frk".$kid;                          
    }
    $tr_kargoextra=doubleval($_POST[$ik_tr]);
    $en_kargopextra=doubleval($_POST[$ik_en]);
    $fr_kargopextra=doubleval($_POST[$ik_fr]);                                                              

$myquery = "INSERT INTO item_kargo(product_id,kargo_id,kargo_priceTR,kargo_priceEN,kargo_priceFR,odemefarkiTR,odemefarkiEN,odemefarkiFR) VALUES ('$next_increment','$kid','$tr_kargop','$en_kargop','$fr_kargop','$tr_kargoextra','$en_kargopextra','$fr_kargopextra')";
   echo "<h2>".$myquery."</h2>";

   $kargo_bilgisi=mysql_query($myquery) or die("!!hata kargooo :".mysql_error());
 }

This is the result of myquery

INSERT INTO item_kargo(product_id,kargo_id,kargo_priceTR,kargo_priceEN,kargo_priceFR,odemefarkiTR,odemefarkiEN,odemefarkiFR) VALUES ('4','1','1','1','1','','','')

INSERT INTO item_kargo(product_id,kargo_id,kargo_priceTR,kargo_priceEN,kargo_priceFR,odemefarkiTR,odemefarkiEN,odemefarkiFR) VALUES ('4','2','2','2','2','','','')

INSERT INTO item_kargo(product_id,kargo_id,kargo_priceTR,kargo_priceEN,kargo_priceFR,odemefarkiTR,odemefarkiEN,odemefarkiFR) VALUES ('4','4','3','3','3','3','3','3')

If can't you get your error from mysql_error() you can "echo" your data with var_dump(), so you can see how your array is build up. This helps me often to figure out my error in some loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM