forumnz Posted May 8, 2009 Share Posted May 8, 2009 I'm having big problems trying to insert an array into a database. Here is what I have so far: <?php $e = '[{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"},{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"}]'; $d = json_decode($e, true); function mysql_insert_array($d) { foreach ($d as $field=>$value) { $fields[] = '`' . $field . '`'; $values[] = "'" . mysql_real_escape_string($value) . "'"; } $field_list = join(',', $fields); $value_list = join(', ', $values); mysql_query("INSERT INTO acc_rec_inv (" . $field_list . ") VALUES (" . $value_list . ")")or die(mysql_error()); } ?> What could be the problem? Thanks Link to comment https://forums.phpfreaks.com/topic/157415-solved-insert-array-into-database/ Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 It's an array in an array. So $value in your foreach loop is actually an array. Link to comment https://forums.phpfreaks.com/topic/157415-solved-insert-array-into-database/#findComment-829835 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 I seem to remember you asked this question before. Ken: How can you tell that, if you don't know how this function is being used? (Which is a question I asked in previous topic, but got no answer). Link to comment https://forums.phpfreaks.com/topic/157415-solved-insert-array-into-database/#findComment-829836 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 I seem to remember you asked this question before. yup, forumnz has about 5 topics related to this one matter. Link to comment https://forums.phpfreaks.com/topic/157415-solved-insert-array-into-database/#findComment-829837 Share on other sites More sharing options...
forumnz Posted May 8, 2009 Author Share Posted May 8, 2009 Yeah, is my function even being used? Link to comment https://forums.phpfreaks.com/topic/157415-solved-insert-array-into-database/#findComment-829845 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Ken: How can you tell that, if you don't know how this function is being used? (Which is a question I asked in previous topic, but got no answer). Because I know what the OP is trying to do from his previous 4 topics on this matter. Failing at that though. forumnz, you have to call the function in that file via mysql_insert_array($d); Link to comment https://forums.phpfreaks.com/topic/157415-solved-insert-array-into-database/#findComment-829847 Share on other sites More sharing options...
forumnz Posted May 8, 2009 Author Share Posted May 8, 2009 I'm sorry, I'm just so confused. I don't really know what you are saying. Would you suggest I keep going with the function I've got or do something different? Link to comment https://forums.phpfreaks.com/topic/157415-solved-insert-array-into-database/#findComment-829860 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.