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 Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 Does it return any errors? Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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? 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.