Title: Undefined offset
Last modified: September 12, 2017

---

# Undefined offset

 *  [sigmoid](https://wordpress.org/support/users/sigmoid/)
 * (@sigmoid)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/undefined-offset-24/)
 * Thanks for writing this plugin, I’ve been using it for a while now.
 * However, look at the following from my log:
 *     ```
       [12-Sep-2017 01:57:18 UTC] PHP Notice:  Undefined offset: 0 in /home/redacted/www/wp-content/plugins/srs-simple-hits-counter/SRS_Simple_Hits_Counter.php on line 92
       [12-Sep-2017 01:57:18 UTC] PHP Notice:  Trying to get property of non-object in /home/redacted/www/wp-content/plugins/srs-simple-hits-counter/SRS_Simple_Hits_Counter.php on line 92
       [12-Sep-2017 01:57:18 UTC] PHP Notice:  Undefined offset: 0 in /home/redacted/www/wp-content/plugins/srs-simple-hits-counter/SRS_Simple_Hits_Counter.php on line 93
       [12-Sep-2017 01:57:18 UTC] PHP Notice:  Trying to get property of non-object in /home/redacted/www/wp-content/plugins/srs-simple-hits-counter/SRS_Simple_Hits_Counter.php on line 93
       ```
   
 * I looked at the code and modified it:
 *     ```
       function update_views_visitors($post_id, $visitors, $views){
           global $wpdb;
           $table_name = $wpdb->prefix.'srs_simple_hits_counter';
           $date = Date("Y-m-d");
           $time = Date("h:i:s");
           $post_data = $wpdb->get_results("SELECT * FROM $table_name WHERE (srs_post_id = '". $post_id ."' AND srs_date = '".$date."' )");
           if($post_data){
               $visitors = $post_data[0]->srs_visitors_count+$visitors;
               $views = $post_data[0]->srs_views_count+$views;
               $wpdb->update($table_name, array('srs_visitors_count' => $visitors, 'srs_views_count' => $views), array('srs_post_id' => $post_id, 'srs_date' => "$date"));
           }else{
               $sql = "INSERT INTO $table_name (<code>srs_id</code>, <code>srs_date</code>, <code>srs_time</code>, <code>srs_post_id</code>, <code>srs_visitors_count</code>, <code>srs_views_count</code>) VALUES (NULL, '".$date."', '".$time."', '". $post_id."', '". $visitors."', '". $views."')";
               $wpdb->insert(
                   $table_name,
                   array(
                       'srs_id' => NULL,
                       'srs_date' => $date,   
                       'srs_time' => $time,
                       'srs_post_id' => $post_id,
                       'srs_visitors_count' =>$visitors,
                       'srs_views_count' => $views
                   )
               );
           }
       }
       ```
   
 * You should not assume that a variable takes always a value, because when it doesn’t
   this opens up the possibility for sql injections.
 * When you have a string that you construct a query with and it has variables, 
   please enclose them in quotes to ensure empty is really empty.
 * thanks
    -  This topic was modified 8 years, 8 months ago by [sigmoid](https://wordpress.org/support/users/sigmoid/).
    -  This topic was modified 8 years, 8 months ago by [sigmoid](https://wordpress.org/support/users/sigmoid/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [sigmoid](https://wordpress.org/support/users/sigmoid/)
 * (@sigmoid)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/undefined-offset-24/#post-9487917)
 * sorry the formatting of the code block being converted by this website is not
   helping, but I guess you understand?
 *  Plugin Author [SandyRig](https://wordpress.org/support/users/sandyrig/)
 * (@sandyrig)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/undefined-offset-24/#post-9532531)
 * Hey, sorry about not responding. I’ll get back to you on this soon.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Undefined offset’ is closed to new replies.

 * ![](https://ps.w.org/srs-simple-hits-counter/assets/icon-256x256.png?rev=3533890)
 * [SRS Simple Hits Counter](https://wordpress.org/plugins/srs-simple-hits-counter/)
 * [Support Threads](https://wordpress.org/support/plugin/srs-simple-hits-counter/)
 * [Active Topics](https://wordpress.org/support/plugin/srs-simple-hits-counter/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/srs-simple-hits-counter/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/srs-simple-hits-counter/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [SandyRig](https://wordpress.org/support/users/sandyrig/)
 * Last activity: [8 years, 8 months ago](https://wordpress.org/support/topic/undefined-offset-24/#post-9532531)
 * Status: not resolved