Jump to content

[SOLVED] Why won't this array/query work?


forumnz

Recommended Posts

<?php
$arr = json_decode($form, true);
$sql = "INSERT INTO acc_rec_invoice SET hash_key = '$hk', inv_key = '$ik', ";
foreach ($arr as $k => $v) {
        $sql .= "$k = '$v', ";
    }
    $sql = substr($sql, 0, -2);
mysql_query($sql);
?>

 

This is the snippet of the code.

When I echo $sql, the output is:

INSERT INTO acc_rec_invoice SET hash_key = '', inv_key = '4d3ace164d2fc28cd7efee6eb28fc1fd', toperson = 'Sam Walker', date1 = '10 May 2009', date2 = '24 May 2009', invnumber = '006', refer = 'Products', tax_s = 'inclusive', ext-comp-1007 = '1122', ext-comp-1008 = 'Nissan Light', ext-comp-1009 = '2', ext-comp-1010 = '69', ext-comp-1011 = 'acc3', ext-comp-1012 = 'tax3'

 

Should work shouldn't it? I do have all the fields in the database.

 

Thanks :)

Link to comment
Share on other sites

Hey forumnz

 

SET is used with an UPDATE query, and INSERT should look something like this for you;

 

INSERT INTO acc_rec_invoice 
(hash_key, inv_key, toperson, date1, date2, invnumber, refer, tax_s, ext-comp-1007, ext-comp-1008, ext-comp-1009, ext-comp-1010, ext-comp-1011, ext-comp-1012)
VALUES ('', '4d3ace164d2fc28cd7efee6eb28fc1fd', 'Sam Walker', '10 May 2009', '24 May 2009', '006', 'Products', 'inclusive', '1122', 'Nissan Light', '2', '69', 'acc3', 'tax3');

 

If you change your code to get your sql to look like that you're laughing!

Link to comment
Share on other sites

That's cool Ken2k7 - you learnt something new! :)

 

Thanks gevans. I did that and I get:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-comp-1007 = '1122', ext-comp-1008 = 'Nissan Light', ext-comp-1009 = '2', ext-co' at line 1

 

I can't see anything wrong though. I don't need to use backticks (I had a similar query yesterday that worked) :)

Link to comment
Share on other sites

Right now I can't see anything wrong either. Are you using phpmyadmin? If so open it up hit the 'SQL' tab and paste this in there

 

INSERT INTO acc_rec_invoice SET hash_key = '', inv_key = '4d3ace164d2fc28cd7efee6eb28fc1fd', toperson = 'Sam Walker', date1 = '10 May 2009', date2 = '24 May 2009', invnumber = '006', refer = 'Products', tax_s = 'inclusive', ext-comp-1007 = '1122', ext-comp-1008 = 'Nissan Light', ext-comp-1009 = '2', ext-comp-1010 = '69', ext-comp-1011 = 'acc3', ext-comp-1012 = 'tax3'

 

that's your original query from post number 1, if the syntax is ok it will insert, if not, well, we'll go from there

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.