Array into table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DavidThor
Forum Newbie
Posts: 3
Joined: Fri Jan 20, 2012 6:05 am

Array into table

Post 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();

User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Array into table

Post 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
Post Reply