0

I want to get value of column and increment it by 1. After increment, update the value in column.For example: In column post_likes the current value is 4. I am incrementing it by 1(like this $var++). After incrementing (value is 5 now), I am updating column value. Now value of column should be 5 which was 4 earlier. I am using below code for this..

$results = $wpdb->get_results( "SELECT * FROM `wp_like` WHERE `id`=1");
global $wpdb;
if(!empty($results)) {
    foreach($results as $row){
        $like = $row->post_likes;         //Fetching value of column
    }
}
if(!empty($like)) {
    $like++;                      //INcrementing by 1
    $wpdb->update( 
        'wp_like', 
        array( 
            'post_likes' => $like    //Updating clumn value
        ), 
        array( 'id' => 1),           //where condition
            '%d'                     //type of value used in where condition
        array( 
        ),
        array('%s')                 //type of value storing in column
    );  
}

But this code is updating value in column by 2 instead of 1. Means, value of column should be 5 now but instead it's 6 now! I have no idea from where this extra increment occure.

To check if I am incrementing twice by mistake I put echo on varaible to check where value is incremeting twice as shown below

$results = $wpdb->get_results( "SELECT * FROM `wp_like` WHERE `id`=1");
global $wpdb,$like;
if(!empty($results)) {
    foreach($results as $row){
        $like = $row->post_likes;
    }
}
echo 'after loop'.$like;              //Returns value 4
if(!empty($like)) {
    echo 'Before increment'.$like;    //Returns value 4
    $like++;                          //INcrementing by 1
    echo 'After increment'.$like;         //Returns value 5
    $wpdb->update( 
        'wp_like', 
        array( 
            'post_likes' => $like
        ), 
        array( 'id' => 1), 
            '%d'
        array( 
        ),
        array('%s') 
    );  

}
echo 'After update'.$like;        //Returns value 5

In comments(in front of echo lines) I have written outcomes of echo. And by this I came to know that value is not incrementing twice by my side. But it's somehow update query is incrementing.

When I remove $like++; from code then no value increments at all. Means value can only be store after incrementing by 2. Why is that? What am I doing wrong?

  • Where is this code running? Could it be being run twice? – Jacob Peattie Nov 29 '17 at 8:21
  • @jacobpeattie It's running in template files of my theme. To check if this code also written somewhere else. I try to remove $like++ from code. Then the value stop incrementing. So the code is being written on one file only. And incrementing value from one line ($like++) only. – Rishabh Nov 29 '17 at 8:25
  • Template files aren't a good place for code like this. Is it at least wrapped in some sort of condition so that it only runs when someone clicks a button or something? – Jacob Peattie Nov 29 '17 at 8:26
  • @jacobpeattie This is exactly I am gonna do. I will put condition. But before doing this I was testing code. And it don't seems to work as expected. – Rishabh Nov 29 '17 at 8:28
  • What template file is it? Could it conceivably be running multiple times on a page? – Jacob Peattie Nov 29 '17 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.