Jump to content


Photo

insert into table using array


  • Please log in to reply
24 replies to this topic

#21 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 04:15 PM

Sorry for big delay, ill just do what you want
I am a PHP Guru, ask me questions if you want to

#22 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 04:22 PM

The for loops
change em

for($u=0; $u<=$num2; $u++)
{
$flds='';
$dat='';
for($i=0; $i<=$num; $i++)
{
$flds.='$fields['.$i'.]';
$dat.='$data['.$i.']';
}
$exec = '$sql = \'INSERT INTO $table['.$u.']($flds) values($dat]);';

// For debugging, you may uncomment this code
// echo($exec);

eval($exec);
mysql_query($sql);
}

I am a PHP Guru, ask me questions if you want to

#23 nel

nel
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 15 August 2006 - 12:02 AM

thanks foe the code..
but this code make m ucclear for example :

$flds.='$fields['.$i'.]';
when i make echo"$flds"; the output contain all of the field and not have comma(,) between field.

$dat.='$data['.$i.']';
when i make echo"$dat"; the output contain all of the data and not have comma(,) between data.

this all cause the data cannot be inserted.

please give me another code...thanks

#24 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 15 August 2006 - 04:42 AM

The easiest way to do this is with the implode() function. See http://www.php.net/implode
<?php
$query = "insert into table_name (" . implode(',',$fields) . ") values '" . implode("','",$data) . "'";
?>

Ken

#25 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 15 August 2006 - 05:35 AM

Uh sorry, Another way
for($u=0; $u<=$num2; $u++)
{
$flds='';
$dat='';
for($i=0; $i<=$num; $i++)
{
$flds.='$fields['.$i'.],';
$dat.='$data['.$i.'],';
}
$flds=substr($flds, 0, -1);
$dat=substr($dat, 0, -1);
$exec = '$sql = \'INSERT INTO $table['.$u.']($flds) values($dat]);';

// For debugging, you may uncomment this code
// echo($exec);

eval($exec);
mysql_query($sql);
}

This should do it
I am a PHP Guru, ask me questions if you want to




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users