Data not retrieved from database

Johan Delft :

I have this piece of code:

<?php
global $wpdb;

$table_name = $wpdb->prefix . 'wpex_programma';
// This retrieves the data from the database
$retrieve_data = $wpdb->get_results( "SELECT Anaam FROM {$table_name}" );
?>
<form action="#" enctype="multipart/form-data" method="post">
    <?php wp_nonce_field( 'set_programma_action', 'set_programma' ); ?>
    <table>
        <?php foreach ( $retrieve_data as $retrieved_data ) { ?>
            <tr>
                <th>Programma:</th>
                <td style="vertical-align: middle;"><?php echo esc_html( $retrieved_data->Anaam ); ?></td>
                <th>
                    <button name="programma" type="submit" value="<?php echo esc_attr( $retrieved_data->Anaam ); ?>">Abonneer</button>
                </th>
            </tr>
        <?php } ?>
    </table>
</form>

<?php
    // Verify nonce and safe the data if the user is logged in.
    // Nonce docs: https://developer.wordpress.org/themes/theme-security/using-nonces/
    if (isset( $_POST['programma'] ) && isset( $_POST['set_programma'] ) && wp_verify_nonce( $_POST['set_programma'], 'set_programma_action' )) {

      $data = filter_input( INPUT_POST, 'programma', FILTER_SANITIZE_STRING );
      $current_user_id = get_current_user_id();

        if ( $current_user_id && ! empty( $data ) ) {
            update_user_meta( $current_user_id, 'programma', $data );
        }
    }
?>

The page does not return the data form the database. Nothing is shown except my navbar and footer. How can I solve this problem?

The code for retrieving the data and displaying it is:

global $wpdb;

$table_name = $wpdb->prefix . 'wpex_programma';
// Dit haalt de data op uit de database
$retrieve_data = $wpdb->get_results( "SELECT Anaam FROM {$table_name}" );
?>
<form action="#" enctype="multipart/form-data" method="post">
    <?php wp_nonce_field( 'set_programma_action', 'set_programma' ); ?>
    <table>
        <?php foreach ( $retrieve_data as $retrieved_data ) { ?>
            <tr>
                <th>Programma:</th>
                <td style="vertical-align: middle;"><?php echo esc_html( $retrieved_data->Anaam ); ?></td>
                <th>
                    <button name="programma" type="submit" value="<?php echo esc_attr( $retrieved_data->Anaam ); ?>">Abonneer</button>
                </th>
            </tr>
        <?php } ?>
    </table>
</form>

I will also show my database structure:

My wpex_usermeta, the choices have to be saved here:

enter image description here

My wpex_programma. All the programs are saved here:

enter image description here

EDIT

echo var_dump($wpdb->prefix);

yields

string(5) "wpex_"

I also did echo var_dump($tablename); and it showed NULL

Lajos Arpad :

Seeing the question and the comment sections, the information seems to be self-contradictory.

If

echo var_dump($wpdb->prefix);

yields

string(5) "wpex_"

that means that due to

$table_name = $wpdb->prefix . 'wpex_programma';

$table_name will have the value of

wpex_wpex_programma

which differs from the expected table name, which is

wpex_programma

so, changing the code to

$table_name = $wpdb->prefix . 'programma';

should solve the issue. Beware of code duplication, you are defining $table_name at different places, so you will need to apply the change everywhere.

You also claim that $table_name is NULL, which is either a printing before it was initialized, or, you have tested it at a place where it was not (properly) initialized at all. You will need to search for the pattern of

$table_name =

in your whole source folder and make sure that it is properly initialized at all the occurrences.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

setting variables against data retrieved from a database

how to shuffle the data retrieved from the database

To create a dictionary of data retrieved from the database

How to print retrieved data from a database

How to format the data retrieved from a database

Priority in retrieving Data from mysql database and filtering the retrieved data

display data retrieved from database from servlet to jsp file

Modal window will not open after paginating retrieved data from the database

Database column data having linebreaks are ignored when retrieved from Hibernate?

Angularjs view not reflecting the data retrieved from pouchDB (in browser database)

Modifying data retrieved from database in asp.net

Reload UiTableView After Data Has Been Retrieved From Database - Swift

Printing the data retrieved from database as response in node.js

how can i save data, retrieved from a url into my database

When is data really retrieved from database using EF Core?

Retrieved data from database and store it in select tag in php

Displaying retrieved firebase data from database in HTML file with Ionic 3

Data on Firestore console differs from the retrieved when consulting the database

Drawing markers on map using data retrieved from my database

Fix the format of the retrieved data from the database using wordpress

Object Retrieved From Database in Laravel

Converting retrieved datetime from database

Data retrieved from database to Dropdown list. User selects option and then send that value to Database?

How to edit retrieved data from database and return table's data to the form's text fields

How to store retrieved data from multiple textfields in SQLite database and display that data on 3 screens in flutter?

"Unwrapping" data retrieved from Firebase

Order of data retrieved from parse

Retrieved data from Localstorage not fresh

Insert to database from datagridview data which is retrieved from another table in C# MSSQL