Jump to content

Why won't my query work?


forumnz

Recommended Posts

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

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.