I’m trying to insert array into database. Why don’t this work?
$array_zone = array();
$array_data = array();
while($row = mysql_fetch_array($keputusan1)){
$array_zone[] = $row['zone'];
$array_data[] = $row['data'];
}
echo "<pre>";
print_r($array_zone);
echo "<br>";
print_r($array_data);
echo "</pre>";
$list_zone = implode(",", $array_zone);
$list_data = implode(",", $array_data);
for($i = 0; $i < 4; $i++)
{
mysql_query("INSERT INTO `db`.`table1` (`id`, `domain`) VALUES ('$list_zone', '$list_data')");
}
Array output before implode:
Array
(
[0] => 270
[1] => 270
[2] => 255
[3] => 255
)
Array
(
[0] => ok.com.
[1] => lo.com.
[2] => i.com.
[3] => k.com.
)
Result I’m getting:

0 key should go with data in 0 key and 1 key should go with data in 1 key and so on… Help me please. Thank you.
You can use INSERT … SELECT. The result returned in
$keputusan1is aSELECTquery. And it certainly returnszoneanddatacolumns. Say the query isSELECT zone, data, columns ... FROM tbl1.If you use INSERT … SELECT the new query would be
This will
SELECTfirst thenINSERTit in one shot.The problems with your code are
$array_zoneand$array_datainstead of$list_zoneand$list_dataif for loop is used. But it should not be as of step 1.