forumnz Posted May 8, 2009 Share Posted May 8, 2009 I'm trying to insert multiple rows into a database using an array, but it doesn't want to work.. why? <?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); //echo $d; $sess_hash = (rand() . rand() . rand() . rand() . rand() . rand() . rand()); $sess_hash = (md5($sess_hash)); $ik = $sess_hash; $sess_hash = (rand() . rand() . rand() . rand() . rand() . rand() . rand()); $sess_hash = (md5($sess_hash)); $lk = $sess_hash; $hk = "hashkey123"; function mysql_insert_array($data) { foreach ($d as $field=>$value) { $fields[] = '`' . $field . '`'; $values[] = "'" . mysql_real_escape_string($value) . "'"; } $field_list = join(',', $fields); $value_list = join(', ', $values); $query = ("INSERT INTO acc_rec_inv (hash_key, inv_key, line_key, " . $field_list . ") VALUES ('$hk', '$ik', '$lk', " . $value_list . ")")or die(mysql_error()); return $query; } ?> Yes there is a valid connection. Thanks Link to comment https://forums.phpfreaks.com/topic/157327-why-wont-my-query-work/ Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 Does it return any errors? Link to comment https://forums.phpfreaks.com/topic/157327-why-wont-my-query-work/#findComment-829240 Share on other sites More sharing options...
forumnz Posted May 8, 2009 Author Share Posted May 8, 2009 Nope, it doesn't, but it doesn't insert anything either. What could be going on? Link to comment https://forums.phpfreaks.com/topic/157327-why-wont-my-query-work/#findComment-829241 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 There's no mysql_query in this code for one thing. Link to comment https://forums.phpfreaks.com/topic/157327-why-wont-my-query-work/#findComment-829242 Share on other sites More sharing options...
forumnz Posted May 8, 2009 Author Share Posted May 8, 2009 Oh whoops! Here is what I have now: <?php function mysql_insert_array($data) { 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()); } ?> It still doesn't work.. why is that? Thanks Link to comment https://forums.phpfreaks.com/topic/157327-why-wont-my-query-work/#findComment-829245 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 How do you call this function? And I think it should be foreach($data not foreach($d Link to comment https://forums.phpfreaks.com/topic/157327-why-wont-my-query-work/#findComment-829249 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Wow, that is one heck of a session hash. Link to comment https://forums.phpfreaks.com/topic/157327-why-wont-my-query-work/#findComment-829394 Share on other sites More sharing options...
w3evolutions Posted May 8, 2009 Share Posted May 8, 2009 Is there database connection code, and if there is does it have any errors? Link to comment https://forums.phpfreaks.com/topic/157327-why-wont-my-query-work/#findComment-829400 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.