0

in WordPress, I have a custom table in which I want to store settings for use in a plugin. The code for the admin page of the plugin, contains a form and prior to displaying the form, I access the database and retrieve the present items.

The problem is, when I click on update, the database is updated with the new item I entered,and it is stored within the database, but when I access the table again, it displays the LAST set of data. The first time through is fine. If I click on the submit button and second time the data is fine.

I am mystified as to why it is not showing the modified data. I have tried flushing, clearing the cache. Do not know why.

The basic code flow is this

  1. Retrieve data from wordpress custom table via get_results() or get_row()
  2. Parse out data into variables, via $row->XXXX or foreach ($rows as $data)
  3. Display in form
  4. Check if the form input field is set, if so, update table via POST
  5. When submit is clicked refresh file and go to item 1, to retrieve data.

The problem is this works the first time through OR if I click on the submit button twice. The data is then displayed correctly. But the FIRST time you click on the submit button, after modifying the input field with different data, it stores the data in the table, but during the refresh process (#1), it does not retrieve what is in the table. It holds the data previously. I have tried clearing the cache

Very confused..

CODE:

global $wpdb;   

$Table_Name = $wpdb->prefix.'member_subscriptions';

$sql_query = $wpdb->prepare("SELECT * FROM $Table_Name", 1) ;

$rows = $wpdb->get_results($sql_query, ARRAY_A);

if (!empty($rows)) 
      {
       foreach ($rows as $data) 
       {
        $mmd_pms_not_logged_in_URL =  $data['notloggedin_url']  ;
      }

  <div class="wrap">
   <form method="POST">  
    Not Logged In<input type="text" size="75" name="MMD_NotLoggedIn" value="<?php echo esc_html($mmd_pms_not_logged_in_URL); ?>"/>

    <input type="submit" value="Update" class="button-primary"/>
</form>
</div>

 <?php

 if ( isset( $_POST['MMD_NotLoggedIn'] ) )
      {
      $mmd_pms_not_logged_in_URL= sanitize_text_field( $_POST['MMD_NotLoggedIn'] );

      $sql_query = $wpdb->prepare("UPDATE $Table_Name SET notloggedin_url=%s WHERE id=1", $mmd_pms_not_logged_in_URL) ;


      $wpdb->query( $sql_query );   
      } 
?>
2
  • I'm reading your question as notloggedin_url is a column in the table, and you're concerned with adding/updating/getting it's value where the row id is '1'. Considering this is a plugin's settings page, is there expected to only be one value (i.e. on one table row where notloggedin_url has a value) for it at any given time?
    – hwl
    May 12, 2017 at 18:05
  • Also, as it would be remiss of me to leave this out, WP does have a plugins settings api that may negate the need for a custom table. It will also help with nonce and sanitization of inputs, etc. [link]codex.wordpress.org/Settings_API
    – hwl
    May 12, 2017 at 18:09

1 Answer 1

0

There's a problem with the basic flow. Just bring the 4th item to the top,

so your new order will be :

  1. Check if the form input field is set, if so, update table via POST
  2. Retrieve data from wordpress custom table via get_results() or get_row()
  3. Parse out data into variables, via $row->XXXX or foreach ($rows as $data)
  4. Display in form
  5. When submit is clicked refresh file and go to item 1, to retrieve data.

The problem is when the submit button is pressed and page is refreshed, first you retrieve the data from the database without updating the data, so you still get the same data-set since the database isn't updated yet, then you update the database.

Simply you have to update/inset the database before anything, then you can retrieve the data from the updated database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.