Title: Incrementing counter issues
Last modified: October 28, 2016

---

# Incrementing counter issues

 *  [stuartb3502](https://wordpress.org/support/users/stuartb3502/)
 * (@stuartb3502)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/incrementing-counter-issues/)
 * Hi – I’m not sure if this is a CFDB issue or not – likely not.
 * But I’m hoping to get some pointers to where I could investigate to find the 
   cause of my issue.
 * I have added a counter which increments each time a form is submitted. I followed
   snippets posted by the developer of the Dynamic Text Extension plugin. My functions.
   php code is this.
 *     ```
       /**** Customer id ****/
       //Define the key to store in the database
       define( 'CF7_COUNTER', 'cf7-counter' );
   
       //Create the shortcode which will set the value for the DTX field
       function cf7dtx_counter(){
           $val = get_option( CF7_COUNTER, 0) + 1;  //Increment the current count
           return $val;
       }
       add_shortcode('CF7_counter', 'cf7dtx_counter');
   
       //Action performed when the mail is actually sent by CF7
       function cf7dtx_increment_mail_counter(){
           $val = get_option( CF7_COUNTER, 0) + 1; //Increment the current count
           update_option(CF7_COUNTER, $val); //Update the settings with the new count
       }
       add_action('wpcf7_mail_sent', 'cf7dtx_increment_mail_counter');
       ```
   
 * I then use a DTX shortcode to have the counter as a hidden field on the CF7 form
   and it gets stored by CFDB.
 * This allows us to have a unique id for each customer contact.
 * The problem is that sometimes (I haven’t figured out reproducible conditions),
   the counter does two odd things. I have seen a couple of instances where the 
   id included in the emails and stored by CFDB is not incremented – two consecutive
   records, same id. Then the next record is +2 – so the counter appears to be incrementing
   but is not in the emails or CFDB.
 * I’ve only seen the second weird behaviour once (today) and that is that the id
   included in an email and in CFDB jumped backwards. i.e. the ids were 134219, 
   134220, 134221, **134220, **134223
 * Again, the counter appears to have incremented but the value in the emails and
   CFDB is different.
 * Note that the contact forms with recorded ids of 134220 were sent at very different
   times (actually 4 hours apart).
 * Any ideas as to what’s going on?
 * Thanks
    Stuart

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

 *  Plugin Author [Michael Simpson](https://wordpress.org/support/users/msimpson/)
 * (@msimpson)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/incrementing-counter-issues/#post-8368585)
 * CFDB hooks into CF7 hook: wpcf7_before_send_mail which is probably happening 
   before the hook you are using.
 *  Thread Starter [stuartb3502](https://wordpress.org/support/users/stuartb3502/)
 * (@stuartb3502)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/incrementing-counter-issues/#post-8369576)
 * Thanks Michael. I’ll investigate the CF7 books and see if I can works out what
   condition would cause the behavior I’m seeing. Thanks for the lead.
 * Stuart

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

The topic ‘Incrementing counter issues’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/contact-form-7-to-database-extension_ffffff.
   svg)
 * [Contact Form DB](https://wordpress.org/plugins/contact-form-7-to-database-extension/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7-to-database-extension/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7-to-database-extension/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7-to-database-extension/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7-to-database-extension/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7-to-database-extension/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [stuartb3502](https://wordpress.org/support/users/stuartb3502/)
 * Last activity: [9 years, 6 months ago](https://wordpress.org/support/topic/incrementing-counter-issues/#post-8369576)
 * Status: not resolved