-3

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

插入编码

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

请帮助我

4

2 回答 2

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() );
}
 }
于 2013-07-11T11:15:56.630 回答
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

$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() );                
 }}}
?>
于 2013-07-11T05:58:49.567 回答