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 Quote Link to comment 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. Quote Link to comment 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). Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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); Quote Link to comment 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? Quote Link to comment 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.