如何在php中将动态数组插入数据库?

时间:2013-07-11 05:34:40

标签: php html arrays phpmyadmin

我正在研究动态数组我需要在数据库中插入这些数组。当我将动态数组插入数据库而不是在一行中显示其覆盖三行。

编码插入

this is my array

$asma[]=GA::select($ga->population,'total',3);

这是插入查询我需要在六列中插入详细信息,因为我的输出包含六个值。

    $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++)
    {


  $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],

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

如果用户从选项中选择one,我需要在1行中存储六个值。如果用户从选项中选择two,则数据库中会显示two行。 这是我的输出

Array
(
    [0] => H Object
        (
            [Voltage] => 18
            [Number] => 1
            [Duration] => 6
        )

    [1] => H Object
        (
            [Voltage] => 38
            [Number] => 4
            [Duration] => 14
        )

    [2] => H Object
        (
            [Voltage] => 38
            [Number] => 4
            [Duration] => 14
        )

它像这样存储在数据库中

Volatge     Duration Number Volatge Duration Number Volatge Duration      Number
18           6       1       18      6        1      18        6        1
38          14       4       38      14       4      38       14        4
38          14       4       38      14       4      38       14        4

我需要像这样存储

 Volatge     Duration Number Volatge Duration Number Volatge Duration      Number
  18           6        1      38      4       14      38     14             4

请帮助我这个

2 个答案:

答案 0 :(得分:0)

您需要将查询移出foreach循环。这是一种方法 -

$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() );                
}

修改

添加第二行,你可以把它改成这样的东西(注意,我看不到你在哪里定义选择1/2,所以我只是把$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 select 2
    $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() );                
}

编辑#2
这是另一个版本。你的for循环无效php for(i=0;i<row_count;i++)。其中每个都应该有$ - for($i=0;$i<$row_count;$i++)。此外,您的sprintf具有错误的变量数量。在您的$q中,您有9 %d,但在$qs,您只会给3.您希望其他6来自哪里。在此我已将您的循环更改为3 - for($i=0;$i<$row_count;$i+=3),并使用$Voltage[$i+1], $Duration[$i+1], $Number[$i+1], $Voltage[$i+2], $Duration[$i+2], $Number[$i+2],填写剩余的6 %d s

$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() );                
 }}}
?>

答案 1 :(得分:0)

<?php 
//code not tested check it

//Logic is changed instead of for looping many times
$data = array();

$j = 0;

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

foreach ( $value as $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 array
 foreach($data as $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() );
}
 }