Undefined offset
-
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
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Undefined offset’ is closed to new replies.