Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5941905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:10:34+00:00 2026-05-22T16:10:34+00:00

How can I increment an int in a cell of a MySQL database? I

  • 0

How can I increment an int in a cell of a MySQL database? I know that auto-increment is no use because I never want to add a new row, just update an existing one. I’m currently using this (POST var used for clarify, is verified in the real code):

$columnToUpdate = 'type'.$_POST['voteid'];
$query = "UPDATE myTable $columnToUpdate = $columnToUpdate+1 WHERE id=1;";
if(!mysql_query($query)) {
    echo json_encode(array('success' => false, 'message' => 'Update failed: '.mysql_error()));
    exit;
}

In the database I have 6 fields, id, type1, type2, type3, type4, type5, and a single row with id set to 1. The intention is to recieve a number (1-5), and build a reference to the correct column before updating the field. That results in Update failed: 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 '=type4+1 WHERE id=1' at line 1, so I guess it’s not getting the field value out properly before it increments.

Once this is working I’m also going to need to decrement a field in the same way, unless its value is 0. So for bonus points, can I do all this in one query or would it be better to split it up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:10:34+00:00Added an answer on May 22, 2026 at 4:10 pm

    I think you’ve missed the keyword ‘SET’ from your query – try

    $query = "UPDATE myTable SET $columnToUpdate = $columnToUpdate+1 WHERE id=1;";

    Edit:

    To do the “decrement unless it’s zero” you could use something like:

    UPDATE myTable SET $columnToUpdate = 
    CASE $columnToUpdate 
        WHEN 0 THEN 0
        ELSE $columnToUpdate - 1
    END CASE
    WHERE id=1;`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to natively auto-increment letters in MySql? If not can you suggest
Assume I have a mysql table: ID - int, auto-increment Num - int Num
I have an database table set up as shown below: copy_blocks id (auto-increment int)
How can I add a unique, auto-incrementing primary key ( IdCustomer ) in Rails?
How can I increment numeric String value? If it is initially 1, I want
Can I create a javascript variable and increment that variable when I press a
In sql-server there is function to auto-increment guid fields. CREATEGUID() how can I do
I know in an actual query, you can use auto_increment=1 but how is this
I have a table that consists of id (auto_increment) number int (can contain values
How can i rewrite this C++ code in Delphi? int *intim = new int[imsize];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.