• Hello,
    Is there a way to check if a data already exists in the database? I need a form that does this check on a text field.
    Thank you

Viewing 1 replies (of 1 total)
  • Hi, gigasdev, the following code snippet from this StackOverflow topic – https://stackoverflow.com/a/57045035/4372244 – may assist you with preventing duplicate field entry values:

    
    //credit to Spencer Shattuck - https://stackoverflow.com/a/57045035/4372244
    function email_already_in_db ( $result, $tags ) {
        // Retrieve the posted form
        $form  = WPCF7_Submission::get_instance();
        $form_posted_data = $form->get_posted_data();
    
        // Get the field name that we want to check for duplicates.
        // I added 'unique' to the beginning of the field name in CF7
        // Checking for that with preg_grep
        $unique_field_name = preg_grep("/unique(\w+)/", array_keys($form_posted_data));
    
        // $unique_field_name comes back as array so the next three lines give us the key as a string
        reset($unique_field_name);
        $first_key = key($unique_field_name);
        $unique_field_name = $unique_field_name[$first_key];
    
        // Check the form submission unique field vs what is already in the database
        $email = $form->get_posted_data($unique_field_name);
        global $wpdb;
        $entry = $wpdb->get_results( "SELECT * FROM wp_cf7_vdata_entry WHERE name LIKE '$unique_field_name' AND value='$email'" );
    
        // If already in database, invalidate
        if (!empty($entry)) {
          $result->invalidate($field_name, 'Your email: '.$email.' already exists in our database.');
          }
        // return the filtered value
      return $result;
    }
Viewing 1 replies (of 1 total)

The topic ‘Prevent sending with multiple data’ is closed to new replies.