$wpdb->query(update table) not working as supposed when passing GET request in the where clause

I have made a table ‘wp_mostfavourite’ in wordpress database, it has got three fields, ‘serialno’, ‘postid’, ‘counts’.

The ‘counts’ is supposeto increased by 1, whenever the following query is processed.

$postids = $_GET['action']; // I tried stripslashes($_GET['action']) as well, the query is not working

$querycount="update wp_mostfavourite set counts = counts+1 where postid = $postids";


After running, this query changes the value of counts for the particular postid in the table to 1, no matter if I write counts = counts+1 or counts = counts+ 5

If I put the value of postid in the where clause directly it works.

$querycount="update wp_mostfavourite set counts = counts+1 where postid = 72270";

Even if I change $postids to any real value instead of GET request it works.


I even tried $wpdb->prepare, but still facing the same issue

$wpdb->query( $wpdb->prepare( 
    update wp_mostfavourite
    set counts = counts+%d
        where postid= %d

$postids // the query is working when I replace it with values such as 72270 
) );

Any possible suggestions will be appreciated.

Source: wpdb

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.