Skip to content Skip to sidebar Skip to footer

How To Insert Dynamic Array Into The Database In Php?

i am working on dynamic array i need to insert these array in the database.when i insert dynamic array into the database instead of displaying in one row its cover three row. codin

Solution 1:

You need to move your query out of the foreach loop. Here is one way to do it -

$Voltage = array();
$Duration = array();
$Number = array();

foreach($asma as $key => $value) 
{

   foreach ( $value as $ind => $hObject ) {
      $Voltage[] = $hObject->Voltage;
      $Duration[] = $hObject->Duration;
      $Number[] = $hObject->Number;               ,
   }

} // endforeach
$q = "INSERT INTO ga (fe, fe1, fe2,fe3,fe4,fe5,fe6,f7,f8, timestamp,username ) VALUES (%d, %d, %d,%d, %d, %d,%d, %d, %d, '%s' ,'$login_session')";
$qs = sprintf( $q, $Voltage[0],$Duration[0],$Number[0],
                   $Voltage[1],$Duration[1],$Number[1],
                   $Voltage[2],$Duration[2],$Number[2],
                   date("Y-m-d H:i:s") );
$result = mysql_query($qs);
if ( ! $result ) {
     die( 'Insert failed ' . mysql_errno() . ' ' . mysql_error() );                
}

edit

to add a second row, you can change it to something like this (note, I don't see where you are defining the select for 1/2, so I just put $user_select) -

$Voltage = array();
$Duration = array();
$Number = array();

foreach($asma as $key => $value) 
{

   foreach ( $value as $ind => $hObject ) {
      $Voltage[] = $hObject->Voltage;
      $Duration[] = $hObject->Duration;
      $Number[] = $hObject->Number;               ,
   }

} // endforeach
$q = "INSERT INTO ga (fe, fe1, fe2,fe3,fe4,fe5,fe6,f7,f8, timestamp,username ) VALUES (%d, %d, %d,%d, %d, %d,%d, %d, %d, '%s' ,'$login_session')";
$qs = sprintf( $q, $Voltage[0],$Duration[0],$Number[0],
                   $Voltage[1],$Duration[1],$Number[1],
                   $Voltage[2],$Duration[2],$Number[2],
                   date("Y-m-d H:i:s") );
if($user_select==2){  //if user select2
    $q = ", (%d, %d, %d,%d, %d, %d,%d, %d, %d, '%s' ,'$login_session')";
    $qs = sprintf( $qs.$q, $Voltage[3],$Duration[3],$Number[3],
                             $Voltage[4],$Duration[4],$Number[4],
                             $Voltage[5],$Duration[5],$Number[5],
                             date("Y-m-d H:i:s") );
}
$result = mysql_query($qs);
if ( ! $result ) {
     die( 'Insert failed ' . mysql_errno() . ' ' . mysql_error() );                
}

edit #2 here is another version. Your for loop is not valid php for(i=0;i<row_count;i++). each of those should have a $ - for($i=0;$i<$row_count;$i++). Also, your sprintf has the wrong amount of variables. In your $q you have 9 %d, but in $qs you only are giving 3. Where do you expect the other 6 to come from. In this I have changed you loop to increase by 3 - for($i=0;$i<$row_count;$i+=3), and used $Voltage[$i+1], $Duration[$i+1], $Number[$i+1], $Voltage[$i+2], $Duration[$i+2], $Number[$i+2], to fill out the remaining 6 %ds

$Voltage = array();
$Duration = array();
$Number = array();

foreach($asma as $key => $value) 
{

   foreach ( $value as $ind => $hObject ) 
   {
      $Voltage[] = $hObject->Voltage;
      $Duration[] = $hObject->Duration;
      $Number[] = $hObject->Number;               

} }// endforeach
for($i=0;$i<$row_count;$i+=3)
{

  $q = "INSERT INTO ga (fe, fe1, fe2,fe3,fe4,fe5,fe6,f7,f8, timestamp,username ) VALUES (%d, %d, %d,%d, %d, %d,%d, %d, %d, '%s' ,'$login_session')";
  $qs = sprintf( $q, $Voltage[$i],$Duration[$i],$Number[$i],
                     $Voltage[$i+1],$Duration[$i+1],$Number[$i+1],
                     $Voltage[$i+2],$Duration[$i+3],$Number[$i+3],

                   date("Y-m-d H:i:s") );
  $result = mysql_query($qs);
  if ( ! $result ) {
     die( 'Insert failed ' . mysql_errno() . ' ' . mysql_error() );                
 }}}
?>

Solution 2:

<?php//code not tested check it//Logic is changed instead of for looping many times$data = array();

$j = 0;

foreach($asmaas$key => $value)
{
$i = 0;

foreach ( $valueas$ind => $hObject )
{
    if($i==0)
    {
        $data[$j]['fe'] = mysql_escape_string($hObject->Voltage);

    }else{

        $data[$j]['fe'.$i] = mysql_escape_string($hObject->Voltage);
    }

    $i++;

    $data[$j]['fe'.$i] = mysql_escape_string($hObject->Duration);
    $i++;

    $data[$j]['fe'.$i] = mysql_escape_string($hObject->Number);
    $i++;

}

$j++;

}// endforeach//multiple arrayforeach($dataas$array)
 {


 //unique array//$array3 = array_merge($Voltage,$Duration,$Number);$fields = implode(',',array_keys($array));

//if you want append any new field append it$fields .= ','.'timestamp,username';

$vals = "'".implode("','",array_values($array))."'";

//if you want append any new values append it$vals .= ",'".time()."','".$login_session."'";


$q = "INSERT INTO ga (".$fields.") VALUES(".$vals.")";

$result = mysql_query($q);
if ( ! $result ) {
    die( 'Insert failed ' . mysql_errno() . ' ' . mysql_error() );
}
 }

Post a Comment for "How To Insert Dynamic Array Into The Database In Php?"