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 6734887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:56:33+00:00 2026-05-26T10:56:33+00:00

I have the following code which is supposed to increment a field value by

  • 0

I have the following code which is supposed to increment a field value by 1 in a prepared PHP mysql statement:

function db_OP_doVote($pdo, $postid, $votetype)
{
    $prepStatement = $pdo->prepare(
        "UPDATE content_posts SET `:votetype` = `:votetype` + 1 WHERE `id` = :id"
    );

    $prepStatement->execute(array(':votetype' => $votetype, ':id' => $postid));

    echo "Success";
}

This however, does nothing. No error is thrown back about incorrect SQL syntax and the script runs to completion, but my field does not update at all.

The values for this script are fed through a jQuery post() to this script:

//isset checking here
$postID = (int)$_POST['id'];
$voteType = $_POST['type'];

if ($voteType == "y")
{
    $trueType = "v-cool";
}
elseif ($voteType == "m")
{
    $trueType = "v-meh";
}
elseif ($voteType == "n")
{
    $trueType = "v-shit";
}
else 
{
    die();
}

$db = db_Connect();

db_OP_doVote($db, $postID, $trueType);

Which also appears to filter the values and send them fine. I can’t wrap my head around what the issue could be. The field being incremented is a BIGINT(20).

What am I missing?

EDIT: Solved the issue.

N.B’s comment hit the nail on the head – binding the column name causes it to be quoted, which invalidates the query. Thanks!

  • 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-26T10:56:34+00:00Added an answer on May 26, 2026 at 10:56 am

    You can’t parameterize column names with PDO. What you can do is have hard-coded values (which you basically already have) and construct the SQL string accordingly. I would check this value in the actual function too though, just to be on the safe side:

    function db_OP_doVote($pdo, $postid, $votetype)
    {
        if( !in_array( $votetype, array( 'v-cool', 'v-meh', 'v-shit' /*, etc. */ ), true ) )
        {
            throw new InvalidArgumentException( 'Unexpected $votetype: ' . $votetype );
            // or simply return false perhaps
        }
    
        $sql = '
            UPDATE content_posts
            SET `' . $votetype . '` = `' . $votetype . '` + 1
            WHERE `id` = :id
        ';
    
        $prepStatement = $pdo->prepare( $sql );
    
        $prepStatement->execute(array(':id' => $postid));
    
        echo "Success";
    }
    

    However, this strategy suggests your database design could use a little more attention. The way you have it now, is that for every type of vote, you have a column. This is not really efficient and/or flexible database design. What happens if you get asked to add another type of vote?

    I’d suggest adding another table, to be more flexible:

    CREATE TABLE `content_post_vote` (
      `content_post_id` int(11) NOT NULL,
      `vote_type` enum('cool','meh','shit') NOT NULL, # using enum() to assure valid vote types 
      `votes` bigint(20) DEFAULT NULL,
      PRIMARY KEY (`content_post_id`,`vote_type`)
    )
    

    Then your query would be something like:

    $sql = '
        INSERT INTO `content_post_vote` (`content_post_id`,`vote_type`,`votes`)
        VALUES( :id, :votetype, 1 )
        ON DUPLICATE KEY UPDATE `votes` = `votes` + 1
    ';
    

    What this does is insert a vote if there is no record for a certain primary key (content_post_id,vote_type) yet, and else update the record with a vote if the record already exists.

    Then to query the database for how many votes of a particular type a particular content_post has gotten, you do this:

     $sql = '
         SELECT `votes` # or perhaps more columns
         FROM `content_post_vote`
         WHERE `content_post_id` = :id AND 
               `vote_type` = :votetype
     ';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, which is supposed to try to parse a given
I have the following code which is supposed to be removing a particular email
i have the following code, which is supposed to give specific functionality but it
I have the following code which supposed to display a TreeView (the original code
I have the following code which does what it's supposed to do: objSQLCommand =
I have the following code, which is supposed to add a shared_ptr instance to
I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return

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.