$wpdb->query(update table) not working as expected 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 supposed to increased by 1, whenever the following query is executed.

$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";

$wpdb->query($querycount);

But 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";

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

$postids=72270; 

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

", 
1, 
$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