phido Posted November 23, 2010 Share Posted November 23, 2010 Hello All, Working on a php / mysql project to do exactly what is described here … http://mechiwiki.com/tech/code/inserting-array-values-into-mysql-table/ Using print_r my array displays as follows: Array ( [0] => Array ( [apples] => 70 [oranges] => 30 [pears] => 90 ) [1] => Array ( [apples] => 30 [oranges] => 20 [pears] => 10 ) [2] => Array ( [apples] => 60 [oranges] => 50 [pears] => 10 ) ... ) The problem I’m having arises in the while statement where I’m trying to ready the values for insertion into the mysql query. Instead of producing a properly formatted set of values for use in the query, the while statement produces this … 70, 30, 90, '', '', ''30, 20, 10, '', '', ''60, 50, 10, '', '', '' Any thoughts on how to fix this, or other ways to approach this, are welcome. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/219588-how-to-insert-an-array-into-a-mysql-table/ Share on other sites More sharing options...
phido Posted November 23, 2010 Author Share Posted November 23, 2010 Note the small symbol near the start of the array should read as: [0] [a zero in brackets] Quote Link to comment https://forums.phpfreaks.com/topic/219588-how-to-insert-an-array-into-a-mysql-table/#findComment-1138473 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2010 Share Posted November 23, 2010 If you use HTML <pre> </pre> tags around your print_r() statement, the output will be formatted and easier to read - echo '<pre>',print_r($your_array,true),'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/219588-how-to-insert-an-array-into-a-mysql-table/#findComment-1138476 Share on other sites More sharing options...
BlueSkyIS Posted November 23, 2010 Share Posted November 23, 2010 can you post your code so we can see what's going on? Quote Link to comment https://forums.phpfreaks.com/topic/219588-how-to-insert-an-array-into-a-mysql-table/#findComment-1138477 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 Assuming the values are inserted into a properly normalized database table, each set in its own record, and each value in its own field . . . <?php $your_array = Array ( 0 => Array ( 'apples' => 70, 'oranges' => 30, 'pears' => 90 ), 1 => Array ( 'apples' => 30, 'oranges' => 20, 'pears' => 10 ), 2 => Array ( 'apples' => 60, 'oranges' => 50, 'pears' => 10 ) ); foreach( $your_array as $value ) { $str[] = "(" . implode(", ", $value) . ")"; } $query = "INSERT INTO `table` (`field_1`, `field_2`, `field_3`) VALUES " . implode( ", ", $str); echo $query; // replace the echo with your query execution after testing . . . ?> Returns: INSERT INTO `table` (`field_1`, `field_2`, `field_3`) VALUES (70, 30, 90), (30, 20, 10), (60, 50, 10) Quote Link to comment https://forums.phpfreaks.com/topic/219588-how-to-insert-an-array-into-a-mysql-table/#findComment-1138487 Share on other sites More sharing options...
phido Posted November 23, 2010 Author Share Posted November 23, 2010 Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/219588-how-to-insert-an-array-into-a-mysql-table/#findComment-1138523 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.