unable to auto increment session value

In my wordpress project i want to run a mysql query in thank you page when user is redirected to it only once a time. But when user refresh it, it should not run. I created a session variable and did the below way. But it did not work. Can somebody help me.

session_start();
$_SESSION['countval']=0;

if($_SESSION['countval']==0){

     $wpdb->insert('wp_user_points',
                   array('user_id' =>$current_user->ID , 
                         'order_id'=>$order_id,
                         'product_sku' =>$new_sku,
                         'pv'=>$points,
                         'added_on'=>date("Y-m-d  H:i:s"),
                         'payment_method'=>$order->payment_method_title,
                         'payment_statues'=>'pending'
                        )
                  );
     $_SESSION['countval']++;

}

Related posts

Leave a Reply

2 comments

  1. you seems to be resetting your counter every time. instead of having this line of code
    $_SESSION['countval']=0;

    make sure you only set it one time by testing to see if its already set.

    if (!isset($_SESSION['countval'])) $_SESSION['countval']=0;
    
  2. Because you set $_SESSION['countval']=0; in first line.
    Use this code:

    session_start();
    if(!isset($_SESSION['countval']))
       $_SESSION['countval']=0;
    
    if($_SESSION['countval']==0){
    
         $wpdb->insert('wp_user_points',
                       array('user_id' =>$current_user->ID , 
                             'order_id'=>$order_id,
                             'product_sku' =>$new_sku,
                             'pv'=>$points,
                             'added_on'=>date("Y-m-d  H:i:s"),
                             'payment_method'=>$order->payment_method_title,
                             'payment_statues'=>'pending'
                            )
                      );
         $_SESSION['countval']++;
       }