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
- Retrieve data from wordpress custom table via get_results() or get_row()
- Parse out data into variables, via $row->XXXX or foreach ($rows as $data)
- Display in form
- Check if the form input field is set, if so, update table via POST
- 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 );
}
?>
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 wherenotloggedin_url
has a value) for it at any given time?