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:
My wpex_programma. All the programs are saved here:
EDIT
echo var_dump($wpdb->prefix);
yields
string(5) "wpex_"
I also did echo var_dump($tablename);
and it showed NULL
In phpmyadmin you can test your queries. If there is something wrong in your query, you won't get a result.
So go to phpmyadmin and open your table and click on the sql tab and test your query until you get a result.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.