Page 1 of 1

Array into table

Posted: Sat Jan 21, 2012 8:42 am
by DavidThor
Hi,

I have the following print_r ($array) that I'm trying to insert into a table.

Array ( [0] => 'xxxx', 'xxxx', 'xxxx', 'xxxx' [1] => 'yyyy', 'yyyy', 'yyyy', 'yyyy')

But the script only adds the values of the final row to the table. Can anybody please help? What am I doing wrong here?

Code: Select all

	foreach ($array as $values)
	{
		$query = "insert into xxxxx values (".$values.")";
	}
	$result = $db->query($query);
	if ($result)
		echo '<p>'.$db->affected_rows.' records insterted .</p>';
	$db->close();


Re: Array into table

Posted: Sat Jan 21, 2012 12:59 pm
by twinedev
You are doing a loop to assign a value to $query, but then only actually executing $query once, AFTER the loop, so it is only executing with the final value of $query (the last set of data).

-Greg