0

Here is my query

// Put it all into the database
mysql_query("INSERT INTO 
    orders (
        item_id, 
        order_id, 
        contact_first_name, 
        contact_last_name,
        contact_email,
        contact_phone,
        business_name,
        business_type,
        business_website,
        business_address,
        business_city,
        business_state,
        business_zip,
        business_phone,
        promo_code,
        add_details
    ) 
    VALUES(
        ". $post_data['item_id'] .", 
        ". $order_hash .",
        ". $post_data['contact_first_name'] .", 
        ". $post_data['contact_last_name'] .",
        ". $post_data['contact_email'] .",
        ". $post_data['contact_phone'] .",
        ". $post_data['business_name'] .",
        ". $post_data['business_type'] .",
        ". $post_data['business_website'] .",
        ". $post_data['business_address'] .",
        ". $post_data['business_city'] .",
        ". $post_data['business_state'] .",
        ". $post_data['business_zip'] .",
        ". $post_data['business_phone'] .",
        ". $post_data['promo_code'] .",
        ". $post_data['add_details'] ."
    ) 
") or die(mysql_error());

Right now if I fill in all the forms so that there is no empty strings it gives me this error. The hash is $order_hash:

Unknown column '76a051b750ce9363cec3df9961d2fd6b' in 'field list'

If I leave some blank then it gives me:

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 ' 1, , , , AL, , , , )' at line 27

I have tried multiple variations with double quotes, single quotes, etc and can't seem to figure it out. This should be a simple insert query that like I have done before but I just have run a muck with this one. Thanks for all of your help :)

1 Answer 1

3

You need to write strings inside quote. Otherwise mysql will take them as column. For example:

...
'". $post_data['contact_first_name'] ."', 
'". $post_data['contact_last_name'] ."',
...

OR more simple (when using doublequote for query)

...
'{$post_data['contact_first_name']}', 
'{$post_data['contact_last_name']}',
...
Sign up to request clarification or add additional context in comments.

3 Comments

THANK YOU! So is there a less messy way or was I just missing the single quotes around?
you just missing single quotes around, also make sure that you filter data before writing in quote, to avoid SQL Injection
yep, I have it filtered above and I just have a clean array before I insert. Thanks once again ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.