Decrement column value on multiple rows with PHP and PDO

Giorgio

I have a MySql products table (accessed via PHP and PDO) with the following structure:

CREATE TABLE IF NOT EXISTS `products` (
  `id` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `order` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Products are shown via order field. When I remove a product, I would like to decrement order values for all the following rows (i.e. if I remove product with order=2, I want to update the following row from order=3 to order=2, the next from order=4 to order=3, and so on...).

I obviously would like to do this query in the most efficient way with PDO. I've found this question and tried the following statement in PhpMyAdmin:

UPDATE products SET order = order - 1 WHERE (order>2)

but it gives me the following error:

#1064 - 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 'order = order - 1 WHERE (order>2)' at line 1

What am I missing? And then, what is the correct way to write it with PDO?

BlitZ

ORDER is the reserved word. Use backticks to escape it:

UPDATE `products` SET `order` = `order` - 1 WHERE (`order` > 2);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PDO Update 1 column multiple rows with array

From Dev

PDO Update 1 column multiple rows with array

From Dev

Insert multiple rows using PHP PDO

From Dev

how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

From Dev

PHP decrement NULL value issue

From Dev

PHP - PDO fetch resultset with column as index and column as value

From Dev

PHP - PDO fetch resultset with column as index and column as value

From Dev

What is the best way to insert multiple rows in PHP PDO MYSQL?

From Dev

PHP - PDO - How to fetch multiple rows with only one query?

From Dev

php pdo select multiple rows and insert to other table with LIMIT

From Dev

Set column value when multiple rows exist

From Java

presto split single column value to multiple rows

From Dev

Return Multiple Rows from Column Value

From Dev

Show COUNT(*) column for value 0 in multiple rows

From Dev

Merge multiple rows based on a single column value

From Dev

Multiple rows with same value in one column

From Dev

Spark split a column value into multiple rows

From Dev

Grouping rows with same column value on multiple columns

From Dev

PHP PDO mySQL query returns column name instead of value

From Dev

PHP PDO Multiple queries doesnt get any value

From Dev

Make a single column value from multiple rows column

From Dev

Return multiple rows grouped by a column and named by other column value

From Dev

Return multiple rows grouped by a column and named by other column value

From Dev

Adding multiple cells in a column based on a value in another column but same rows

From Dev

Split pandas column into multiple rows, where splitting is on the value of another column

From Dev

How to get multiple column's rows value in a single column?

From Dev

Value return when no rows in PDO

From Dev

If condition to check value of multiple column array php

From Dev

Column name and value get PDO

Related Related

  1. 1

    PDO Update 1 column multiple rows with array

  2. 2

    PDO Update 1 column multiple rows with array

  3. 3

    Insert multiple rows using PHP PDO

  4. 4

    how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

  5. 5

    PHP decrement NULL value issue

  6. 6

    PHP - PDO fetch resultset with column as index and column as value

  7. 7

    PHP - PDO fetch resultset with column as index and column as value

  8. 8

    What is the best way to insert multiple rows in PHP PDO MYSQL?

  9. 9

    PHP - PDO - How to fetch multiple rows with only one query?

  10. 10

    php pdo select multiple rows and insert to other table with LIMIT

  11. 11

    Set column value when multiple rows exist

  12. 12

    presto split single column value to multiple rows

  13. 13

    Return Multiple Rows from Column Value

  14. 14

    Show COUNT(*) column for value 0 in multiple rows

  15. 15

    Merge multiple rows based on a single column value

  16. 16

    Multiple rows with same value in one column

  17. 17

    Spark split a column value into multiple rows

  18. 18

    Grouping rows with same column value on multiple columns

  19. 19

    PHP PDO mySQL query returns column name instead of value

  20. 20

    PHP PDO Multiple queries doesnt get any value

  21. 21

    Make a single column value from multiple rows column

  22. 22

    Return multiple rows grouped by a column and named by other column value

  23. 23

    Return multiple rows grouped by a column and named by other column value

  24. 24

    Adding multiple cells in a column based on a value in another column but same rows

  25. 25

    Split pandas column into multiple rows, where splitting is on the value of another column

  26. 26

    How to get multiple column's rows value in a single column?

  27. 27

    Value return when no rows in PDO

  28. 28

    If condition to check value of multiple column array php

  29. 29

    Column name and value get PDO

HotTag

Archive