Update database from input field generated under "foreach..."

Hi,

On wordpress i have a plugin that generate licence keys when an order is completed on woocommerce.

I want to associate an environnement key to this licence key.
I created a separate database where i can store my data.

As you can see on the attached picture… I have a page where the user can see his licences and add or modify the “environnement key”.

What i would like to do is when the user change they key from the input, and click on the button the function save the code environnement and update the field in the database.

The problem is that since we are in a “foreach ($licenseKeyData[‘licenses’] as $license)” i don’t how to keep the $license in order to update the corresponding field…

Can you understand what I mean ?
If you do, i really appreciate your help.

:slight_smile:

The database…

function Init_DB_WooComApi() {
  global $wpdb;
  $charset_collate = $wpdb->get_charset_collate();
  $table_name = $wpdb->prefix . 'WooComApi';
  
  $sql = "CREATE TABLE `$table_name` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `order_id` BIGINT(20) DEFAULT NULL,
  `license_key` LONGTEXT DEFAULT NULL,
  `code_env` LONGTEXT DEFAULT '' NOT NULL,
  `link_keyfile` varchar(55) DEFAULT '' NOT NULL,
  PRIMARY KEY (id)
  ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  ";
  if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
  }
}

the code…

<?php foreach ($licenseKeys as $productId => $licenseKeyData): ?>
    <?php $product = wc_get_product($productId); ?>

    <h3 class="product-name">
        <?php if ($product): ?>
            <a href="<?php echo esc_url(get_post_permalink($productId)); ?>">
                <span><?php echo ($licenseKeyData['name']); ?></span>
            </a>
        <?php else: ?>
            <span><?php echo __('Product', 'license-manager-for-woocommerce') . ' #' . $productId; ?></span>
        <?php endif; ?>
    </h3>

  
        <?php
        /** @var LicenseResourceModel $license */
        foreach ($licenseKeyData['licenses'] as $license):
            
           //add get the code env + link
            //$code_env = "ici le code env tmp";
            $link_tokey = "www.linktokey.com" ;
            $order      = wc_get_order($license->getOrderId());
            $current_license = $license->getDecryptedLicenseKey();

            //get the code env
            global $wpdb;
            $sql = "SELECT code_env FROM wp_woocomapi WHERE license_key = %s";
            $code_envir = $wpdb->get_var( $wpdb->prepare( $sql, $current_license ) );       
           
            $sql = "SELECT link_keyfile FROM wp_woocomapi WHERE license_key = %s";
            $link_keyfile = $wpdb->get_var( $wpdb->prepare( $sql, $current_license ) );    

            if (empty($link_keyfile)) {
            $Status_keyfile="Fichier de license non disponible pour le moment";
            } else {
           $Status_keyfile="Fichier de license disponible";
            }
            
            ?>
            
            <table class="shop_table shop_table_responsive my_account_orders">
            <thead>
            <tr>
           <th class="license-key"><?php _e('License key', 'license-manager-for-woocommerce'); ?></th>
           <th class="license-key"><?php _e('Actions', 'license-manager-for-woocommerce'); ?></th>
           </tr>
           </thead>
           <tbody>

<! –– Ligne pour la clé logiciel  ––>
            <tr>
                <td><span class="lmfwc-myaccount-license-key"><?php echo $current_license; ?></span></td>
                <td class="license-key-actions">
                    <a href="<?php echo esc_url($order->get_view_order_url()); ?>" class="button view"><?php _e('Order', 'license-manager-for-woocommerce');?></a>
                </td>
            </tr>
<! –– Ligne pour le code environnement  ––>

            <tr scope="row">
            <th scope="row"><label for="single_code_env"><?php esc_html_e('Environment code', 'license-manager-for-woocommerce');?></label></th>
            </tr> 

            <tr scope="row">
            <td>
                <input name="env_code" id="env_code_unique" class="regular-text" type="text" value="<?php echo esc_html($code_envir); ?>">
                <p class="description"><?php esc_html_e('Info/comment sur le code d environnement', 'license-manager-for-woocommerce');?></p>
            </td>

              <td class="license-key-actions">
                <input type="submit" name="update_code_env" class="button view" value= "<?php _e('Mettre à jour le code', 'license-manager-for-woocommerce');?>"
                </td>       
        </tr>

<! –– Ligne pour le fichier de clé  ––>
        <tr scope="row">
            <th scope="row"><label for="Link_tokey"><?php esc_html_e('Link to software key', 'license-manager-for-woocommerce');?></label></th>
            </tr> 

            <tr scope="row">
            <td>
                <?php echo $Status_keyfile ?>
            </td>

              <td class="license-key-actions">
                    <a href="<?php echo esc_url($order->get_view_order_url()); ?>" class="button view"><?php _e('Download the key', 'license-manager-for-woocommerce');?></a>
                </td>

        </tr>

   

    </tbody>
    </table>
    <p></p>
        <?php endforeach; ?>            
<?php endforeach; ?>


</html>

  
<?php

if($_GET){
    if(isset($_GET['update_code_env'])){
        update_code();
        }
}
    function update_code()
    {
    
    // save  the input in the database...
       
    }
?>

I’m not really clear on how your current set up works? (submits whole page/data after initial output and a button is clicked?)

Perhaps assign each button a function that calls an ajax script that logs/saves/update the field the user wants to input?

Make a separate form for each license and have the license ID as a hidden parameter. Then your code can update the environment code attached to that license ID.

Hi,
Thank you for your answer.
I like the idea of a hidden parameter.
I will try with
I didn’t know how about the separate form. I will search about it…
Thank you :wink:

Here is what i have done so far…

    <td>
            <?php $current_license = $license->getDecryptedLicenseKey(); ?>
          <iframe name="votar" style="display:none;"></iframe>
           <form method="post" target="votar">
           <input type='hidden' name='code_licence' value="<?php echo esc_html($current_license);?>">
           <input name="env_code" class="woocom_keylicence" type="text" value="<?php echo esc_html($code_envir);?>">
           </td><td>
           <input type='submit' value ='Mettre à jour le code env' class="button view" name='validate' id='validate'>
           </form>
           </td>

 if (isset($_POST))
 {

$code_licence = $_POST['code_licence'];
$env_code = $_POST['env_code'];
$table_name = $wpdb->prefix . 'WooComApi';
 
global $wpdb;
$wpdb->update( $table_name,
[ 'code_env' => $env_code ],
[ 'license_key' => $code_licence ]

);

}
Sponsor our Newsletter | Privacy Policy | Terms of Service