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
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
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
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
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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.