Forum Moderators: coopster

Message Too Old, No Replies

problem inserting into mysql table

need help with shopping cart script

         

HelenDev

3:01 pm on Feb 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am developing a shopping cart but have run into a problem. Here is part of my code...

$putitems = mysql_query("INSERT INTO cart (UserID,Item,Manufacturer,Model,Price) VALUES ('$userinfo','$additem','$addproducts[manufacturer]','$addproducts[model]','$addproducts[price]')");

...which works fine but I want to also add the product description, so I do this...

$putitems = mysql_query("INSERT INTO cart (UserID,Item,Manufacturer,Model,Desc,Price) VALUES ('$userinfo','$additem','$addproducts[manufacturer]','$addproducts[model]','$addproducts[briefdesc]','$addproducts[price]')");

...and it refuses to insert the record at all. Does anyone have any suggestions as to why this is happening? I'm hoping it's a foolish syntax error but I just can't spot it.

Helen.

coopster

3:27 pm on Feb 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, HelenDev!

Are you receiving any error messages?
Do you have a primary key defined for your

cart
table?

HelenDev

3:38 pm on Feb 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers coopster.

No error message, it just doesn't insert any data for that record into cart.

Cart does have a primary key which is an autonumber field called ID.

Helen.

justageek

3:44 pm on Feb 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unless you check for mysql errors you won't see any. You may want to wrap your text data with mysql_escape_string(). To check for errors from mysql you have to echo out mysql_error().

JAG

scumm_bar2

3:45 pm on Feb 13, 2004 (gmt 0)

10+ Year Member



I can tell you right now that using 'Desc' as a name for a column can cause problems - I've had it myself. Name it something more unique and try that.

ikbenhet1

3:46 pm on Feb 13, 2004 (gmt 0)

10+ Year Member



have you tries escaping the string with mysql_escape_string($addproducts[breifdesc])?

HelenDev

3:52 pm on Feb 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you scumm_bar2, you are a truly wonderful person!

I changed Desc to prodesc, and now all is working perfectly.

Thanks everyone else who replied as well, I will be make a note of your advice for next time I have a problem.

Helen.

HelenDev

3:57 pm on Feb 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Of course! I just realised that Desc might cause confusion as in 'ORDER BY DESC'. I was just thinking of it as short for description. Duh.

Helen.

scumm_bar2

4:08 pm on Feb 13, 2004 (gmt 0)

10+ Year Member



Don't worry, I had it littered all over my tables for a long time before I discovered it. Mostly I use backquotes in my queries like (`id`, `desc`, `price`) so it must have passed through. Several months later I noticed my database wouldn't backup at all. Yeah, 2 days of my life spent on that one.... :)

jatar_k

6:20 pm on Feb 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Treatment of Reserved Words in MySQL [mysql.com]

DESC is in there

henry0

12:16 am on Feb 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jatar_K

that list contains one word I was planing to use tomorrow :)

regards

Henry