简体   繁体   English

未从数据库中检索到的数据

[英]Data not retrieved from database

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:我的 wpex_usermeta,选择必须保存在这里:

在此处输入图片说明

My wpex_programma.我的 wpex_programma。 All the programs are saved here:所有程序都保存在这里:

在此处输入图片说明

EDIT编辑

echo var_dump($wpdb->prefix);

yields产量

string(5) "wpex_"字符串(5)“wpex_”

I also did echo var_dump($tablename);我也做了 echo var_dump($tablename); and it showed NULL它显示为NULL

In phpmyadmin you can test your queries.在 phpmyadmin 中,您可以测试您的查询。 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.因此,转到 phpmyadmin 并打开您的表,然后单击 sql 选项卡并测试您的查询,直到获得结果。

Seeing the question and the comment sections, the information seems to be self-contradictory.看到问题和评论区,信息似乎自相矛盾。

If如果

echo var_dump($wpdb->prefix);

yields产量

string(5) "wpex_"字符串(5)“wpex_”

that means that due to这意味着由于

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

$table_name will have the value of $table_name的值为

wpex_wpex_programma wpex_wpex_programma

which differs from the expected table name, which is这与预期的表名不同,即

wpex_programma 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.注意代码重复,您在不同的地方定义了$table_name ,因此您需要在任何地方应用更改。

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.您还声称$table_nameNULL ,这要么是在初始化之前进行打印,要么是在根本没有(正确)初始化的地方对其进行了测试。 You will need to search for the pattern of您将需要搜索的模式

$table_name = $table_name =

in your whole source folder and make sure that it is properly initialized at all the occurrences.在整个源文件夹中,并确保它在所有出现的情况下都正确初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM