Okay
  Public Ticket #1933742
editing data from contact form 7 on frontend
Closed

Comments

  • godwin_haueis started the conversation

    Dear SupportTeam,

    I'm collecting data on my WP-website with "Contact Form 7". These data (guest lists) were stored in the WP-database via the Plugin "Advanced CF7 DB".

    I managed to show the stored data with "wpDataTables". Therefor I had to use a pivot query in mysql.

    SELECT cf7_vdata_entry.`data_id` AS 'ID'
         , MAX(CASE WHEN name='aaa' THEN value END) aaa
         , MAX(CASE WHEN name='bbb'   THEN value END) bbb
         , MAX(CASE WHEN name='ccc' THEN value END) ccc
    FROM cf7_vdata_entry
    GROUP BY data_id

    Is there a better way to to this?


    I enabled the editing function in "wpDataTables", but I always get errors, when I tried to store changed data: "Unknown column", "Error while trying to save the table!" Probably this is coused by the pivot query.

    Is there any posibility to edit data from contact forms (generated by "Contact Form 7" and stored by an plugin) with "wpDataTables"?

    I hope there is any solution. Otherwise I buyed "wpDataTables" unsuccessfully.

    Best wishes, Godwin

  •  2,572
    Aleksandar replied

    Hello Godwin.

    Thank you for your purchase.

    Please note that MySQL query constructor is not an ‘ultimate generator’, it should be treated as helper for constructing a suggestion of a query, by trying to ‘guess’ what you want. We are constantly working to improve it, but SQL is such a complicated and flexible language that fully automating the process for constructing queries is hardly possible. Consequently, the more complicated your request is, the higher is the probability that it will not return exactly what you need.

    Now, what you can try is to create the copy of that table in PHPMyAdmin. Something like this:

    CREATE `newtable` AS SELECT cf7_vdata_entry.`data_id` AS 'ID'
         , MAX(CASE WHEN name='aaa' THEN value END) aaa
         , MAX(CASE WHEN name='bbb'   THEN value END) bbb
         , MAX(CASE WHEN name='ccc' THEN value END) ccc
    FROM cf7_vdata_entry
    GROUP BY data_id

    And then, when you create a table in wpDataTables, you are using a simple query:

    SELECT * FROM newtable

    Please try this and let me know if the outcome is more responsive.

    Best regards.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • godwin_haueis replied

    Hello Aleksandar,

    Thanks fou your answer. By your suggestion I chose a different solution.
    I wrote an simple action hook for contact form 7, wich creates a table in an usable structure.

    <?php defined('ABSPATH') or die("Thanks for visting");
      /**
      * Plugin Name: CF7toDB
      * Description: Hiermit werden Formulardaten aus CF7 in die Datenbank geschrieben.
      */
      function form_to_db($wpcf7) {
        // Varaibelen für Formulardaten deklarieren
        $submission = WPCF7_Submission::get_instance();
        $filterliste = array("wpcf7", "g-recaptcha");
        // Variablen für Tabelle deklarieren
        global $wpdb;
        $tabellenprefix = "formdata";
        $tabellenspalten = array();
        $charset_collate = $wpdb--->get_charset_collate();
        // weiter, wenn Formulardaten vorhanden sind
        if ( $submission ) {
          // Daten aus dem Formulardaten filtern (unset) und vor SQL Injection schützen (esc_sql)
          $formulardaten = $submission->get_posted_data();
          foreach ($formulardaten as $feldname => $feldinhalt) {
            $formulardaten[$feldname] == esc_sql($feldinhalt);
            foreach ($filterliste as $filterwort) if (stripos($feldname, $filterwort) !== false) unset ($formulardaten[$feldname]);
          }
          // Daten für Tabelle aufbereiten (str_replace) und vor SQL Injection schützen (esc_sql)
          $tabellenname = str_replace(" ", "_", $tabellenprefix."_".$wpcf7->title());
          $tabellenname = esc_sql($tabellenname);
          // Tabelle anlegen, falls noch nicht vorhanden
          $table_exists = $wpdb->get_var("SHOW TABLES LIKE `{$tabellenname}`");
          if (!$table_exist) {
            $sql = "CREATE TABLE `{$tabellenname}` (
              `id` mediumint NOT NULL AUTO_INCREMENT,
              `erstellt` DATETIME DEFAULT CURRENT_TIMESTAMP,
              PRIMARY KEY (`id`)
            ) $charset_collate;";
            require_once(ABSPATH.'wp-admin/includes/upgrade.php');
            dbDelta($sql);
          }
          // Spalten in Tabelle anlegen, falls noch nicht vorhanden
          $tabele_desc = $wpdb->get_results("DESC `{$tabellenname}`");
          foreach ($tabele_desc as $inhalt) if ($inhalt->Field!="") $tabellenspalten[]=$inhalt->Field;
          foreach ($formulardaten as $feldname => $feldinhalt) {
            if (!in_array($feldname, $tabellenspalten)) {
              $sql = "CREATE TABLE `{$tabellenname}` (`{$feldname}` TEXT) {$charset_collate};";
              require_once(ABSPATH.'wp-admin/includes/upgrade.php');
              dbDelta($sql);
            }
          }
          // Formulardaten in Tabelle schreiben
          $dbcheck = $wpdb->insert($tabellenname, $formulardaten);
          // Hinweistext ausgeben bei Fehlern
          if ($wpdb->last_error !== ""){
            $message = "FEHLER #0001: Formular konnte nicht gespeichert werden. (".htmlspecialchars($wpdb->last_error, ENT_QUOTES).")";
            add_filter('wpcf7_display_message', create_function('$a', 'return "'.$message.'";'));
          } 
          elseif ($dbcheck!=1){
            $message = "FEHLER #0002: Formular konnte nicht gespeichert werden.";
            add_filter('wpcf7_display_message', create_function('$a', 'return "'.$message.'";'));
          } 
        }
      }
      // Hook aktivieren
      add_action( 'wpcf7_before_send_mail', 'form_to_db' );
    ?>

    A simple query in wpDataTables now works perfect whith the database table:

    SELECT * FROM tabellenname

    Greate! Thanks fou your suggestion.

    Best regards,
    Godwin

  • godwin_haueis replied

    Hello Aleksandar,

    I habe a new problem: When useing wpDataTables to display the data, I got 10 warnings like this:

    Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in .../wp-content/plugins/wpdatatables/lib/greenlion/php-sql-parser/src/PHPSQLParser/processors/SQLProcessor.php on line 148
    Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"?in .../wp-content/plugins/wpdatatables/lib/greenlion/php-sql-parser/src/PHPSQLParser/processors/SQLProcessor.php on line 160
    and so on ...

    I`m running PHP 7.3. What is the solution to fix this. I could manualy cange all "continue" commands to "break". Is there a simpler solution? I appreciate your help with this.

    Best regards,
    Godwin

  •  2,572
    Aleksandar replied

    Hi Godwin.

    These should be just warnings, and should not affect the table or data in it in any way. Can you please confirm this?

    I will forward the issue to our development team, so they can look for a fix.

    Best regards.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • godwin_haueis replied

    Hi Aleksandar,
    yes, just warnings. They don't  affect the table or data.


    The warning is caused by PHP version 7.3. The "continue" command ist deprecated.

    Best regards.

  •  2,572
    Aleksandar replied

    Right, Godwin, I see.

    Yeah, that has already been forwarded to our development team, and they are looking for a fix for one of our next releases.

    Best regards.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables