Jump to content

Array insert


forumnz

Recommended Posts

I wish to insert an array into a database. I am really new to this so please help me.

 

Here is my array output:

Array ( [0] => Array ( [qty] => 66 [price] => 5 [item] => ggg [des] => g [acc] => h [tax] => h ) [1] => Array ( [qty] => 66 [price] => 5 [item] => ggg [des] => g [acc] => h [tax] => h ) ) 

 

What can I do?

Link to comment
https://forums.phpfreaks.com/topic/157426-array-insert/
Share on other sites

Ok thanks I'm starting to understand. I'm just taking it step by step at the moment, here is what I have:

 

<?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);
$d = (print_r($d));


foreach ($d as $val)
{
echo $val . " ";
}

?>

 

I know the error has something to do with the print_r part, but how can I fix it?

Thanks :)

 

Link to comment
https://forums.phpfreaks.com/topic/157426-array-insert/#findComment-829911
Share on other sites

Oh I see - getting there!

 

I have this at the moment (snippet):

<?php
foreach ($d as $val)
{

foreach ($val as $arr)
{

$sess_hash = (rand() . rand() . rand() . rand() . rand() . rand() . rand());
$sess_hash = (md5($sess_hash));
$lk = $sess_hash;

mysql_query("INSERT INTO acc_rec_inv (hash_key, inv_key, line_key, item, des, qty, price, acc, tax) VALUES ('$hk', '$ik', '$lk', '$arr')")or die(mysql_error());

}
}
?>

But I get this error:

Column count doesn't match value count at row 1

 

Should I have the query outside of the second array?

Link to comment
https://forums.phpfreaks.com/topic/157426-array-insert/#findComment-829932
Share on other sites

Oops! :)

 

This is better:

<?php
foreach ($d as $val)
{

foreach ($val as $arr)
{
$in .= "'$" . $arr . "', ";

}
$sess_hash = (rand() . rand() . rand() . rand() . rand() . rand() . rand());
$sess_hash = (md5($sess_hash));
$lk = $sess_hash;

echo $in;
mysql_query("INSERT INTO acc_rec_inv (hash_key, inv_key, line_key, item, des, qty, price, acc, tax) VALUES ('$hk', '$ik', '$lk', '$in')")or die(mysql_error());
}
?>

 

That sort of works, but since I have 2 rows to insert, I have 2 times as much data, so I need to somehow split the $in between ().

 

Is this easy?

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/157426-array-insert/#findComment-829937
Share on other sites

Thanks for the help. I am almost there. I have the right amount of columns etc..

Just when there is more than one set of data, in adds more fields within the VALUES (here) - that means that there isn't the right amount of columns...

 

Here is what I have:

 

<?php
foreach ($d as $val)
{

foreach ($val as $arr)
{
$in .= "'" . $arr . "', ";
$ina = "('$hk', '$ik', '$lk', " . $in . ")";
$l = array(", )");
$ina = str_replace($l, ")", $ina);
}
$sess_hash = (rand() . rand() . rand() . rand() . rand() . rand() . rand());
$sess_hash = (md5($sess_hash));
$lk = $sess_hash;

echo $ina;
mysql_query("INSERT INTO acc_rec_inv (hash_key, inv_key, line_key, item, des, qty, price, acc, tax) VALUES $ina")or die(mysql_error());
}
?>

 

What do you think? :) Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/157426-array-insert/#findComment-829975
Share on other sites

Given this....

 

]
<?php
$data = '[{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"},{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"}]';
$array = json_decode($data, true);

foreach ($array as $innerarray) {
    $sql = "INSERT INTO acc_rec_inv SET ";
    foreach ($innerarray as $k => $v) {
        $sql .= "$k = '$v', ";
    }
    $sql = sustr($sql, 0, -2);
    // at this point in time your query will look like "INSERT INTO acc_rec_inv SET qty = '66', price = '5', item = 'ggg', des = 'g', acc = 'h', tax = 'h'"
    // execute the query.
}

?>

 

Im not sure where your planning on getting 'hash_key, inv_key, line_key' from but you should be able to sort it from here.

Link to comment
https://forums.phpfreaks.com/topic/157426-array-insert/#findComment-829988
Share on other sites

Of course if your guaranteed that the data will be in the same order within the array at all times you could also have used implode.

 

<?php
$data = '[{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"},{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"}]';
$array = json_decode($data, true);

foreach ($array as $innerarray) {
    $sql = "INSERT INTO acc_rec_inv (qty, price, item, des, acc, tax) VALUES ('" . implode("','", $innerarray) . "')";
    // execute query.
}

?>

 

Which would be more efficient.

Link to comment
https://forums.phpfreaks.com/topic/157426-array-insert/#findComment-829990
Share on other sites

It looks like:

INSERT INTO acc_rec_inv SET qty = '66', price = '5', item = 'ggg', des = 'g', acc = 'h', tax = 'h'INSERT INTO acc_rec_inv SET qty = '66', price = '5', item = 'ggg', des = 'g', acc = 'h', tax = 'h'

 

I guess then I should just change it to complete each query separately?

Link to comment
https://forums.phpfreaks.com/topic/157426-array-insert/#findComment-829993
Share on other sites

Hey thanks :) this is the query I have at the moment.. The $sql echoed:

mysql_query("INSERT INTO acc_rec_inv SET qty = '66', price = '5', item = 'ggg', des = 'g', acc = 'h', tax = 'h'"); mysql_query("INSERT INTO acc_rec_inv SET qty = '66', price = '5', item = 'ggg', des = 'g', acc = 'h', tax = 'h'");

 

It looks right to me, but how can I actually 'run' the variable instead of echoing it?

Link to comment
https://forums.phpfreaks.com/topic/157426-array-insert/#findComment-830021
Share on other sites

<?php
$data = '[{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"},{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"}]';
$array = json_decode($data, true);

foreach ($array as $innerarray) {
    $sql = "INSERT INTO acc_rec_inv SET ";
    foreach ($innerarray as $k => $v) {
        $sql .= "$k = '$v', ";
    }
    $sql = sustr($sql, 0, -2);
    mysql_query($sql);
}

?>

Link to comment
https://forums.phpfreaks.com/topic/157426-array-insert/#findComment-830024
Share on other sites

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.