Tuesday 4 September 2018

PHP SQL Query does not work after I add some other columns to insert in

I'm new here so let me know if I'm doing something wrong or am supposed to do something that I wasn't aware of!

My question: I am running an sql query through PHP and everything is fine. It's the following query:
$query = "INSERT INTO products ( name, type, brand, price, discount, dateAdded, categories, status, description, code, weight, insurance, frame, fork, transmission, brakes, isDiscount, isPromo, gender ) VALUES ( '{$name}' , '{$sub}' , '{$status}' , '{$brand}' , '{$price}' , '{$discount}' , NOW() , '{$category}' , '{$status}' , '{$description}' , '{$id}' , '{$weight}' , '{$insurance}' , '{$frame}' , '{$fork}' , '{$transmission}' , '{$brakes}' , '{$is_discount}' , '{$promo}' , '{$gender}' )";

But now, I added a few more columns to it and it stopped working... Does anyone see whatever it is that I'm missing? The new query is:
$query = "INSERT INTO products ( name, type, brand, price, discount, dateAdded, categories, status, description, code, weight, insurance, frame, fork, transmission, brakes, isDiscount, isPromo, gender, frontlight, backlight, stem, seatpost, lock, year ) VALUES ( '{$name}' , '{$sub}' , '{$status}' , '{$brand}' , '{$price}' , '{$discount}' , NOW() , '{$category}' , '{$status}' , '{$description}' , '{$id}' , '{$weight}' , '{$insurance}' , '{$frame}' , '{$fork}' , '{$transmission}' , '{$brakes}' , '{$is_discount}' , '{$promo}' , '{$gender}' , '{$front}' , '{$back}' , '{$stem}' , '{$seat}' , '{$lock}' , '{$year}' )";

It's really weird, since there's really no difference aside from a few extra columns to insert into! Thanks in advance!!
EDIT: I can't upvote yet because I don't have enough reputation. So, I would like to but can't, sorry! From what I've seen on stackoverflow you guys really appreciate the upvotes and I can understand that :)
EDIT: Thanks a lot everyone. I'm still new to MySQL and wasn't aware of lock being a reserved word. I changed that field's name and now everything works fine. What's the best way for me to give credit to everyone who helped??

You have to escape lock since it is a reserved word:
INSERT INTO ... `lock`, year) VALUES (...)

And as in Marco's answer stated: You have more fields in your VALUES brackets than you define columns to insert. The error is here:
INSERT INTO products (name, type, ... )
VALUES ( '{$name}' , '{$sub}' , '{$status}', ...)";

Either you need to add another column name before or after type or remove one from your values list: either sub or status

0 comments:

Post a Comment