简体   繁体   中英

MYSQL Updating multiple columns using variables

I used this query to insert all my values into this database:

INSERT INTO products ($fields) VALUES ($values)

However, I try to use the same format for UPDATE:

UPDATE products SET ($fields) VALUES ($values) WHERE sku = '$checksku'

...and am getting thrown a syntax error:

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 '('product,make,model,' at line 1

I can't figure it out. Would appreciate any help. Thanks.

UPDATE syntax is different than INSERT syntax. An example of UPDATE would be:

"UPDATE products SET field1 = 'value1', field2 = '$val2', field3 = 5 WHERE sku = '$checksku'"  

Though this may be insecure. You should look into parameterized queries .

INSERT INTO products ($fields) VALUES ($values) ON DUPLICATE KEY UPDATE field = VALUES(field), ...

Don't forgot about unique or primary key

you need an =

UPDATE products SET ($fields) = $values WHERE sku = '$checksku'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM