PHP MySQL query - return results for current user

2019-09-07 20:02发布

I'm currently building a website using wordpress that has a personality quiz on it. There is a results page that retrieves the value of the users last submission. I have managed to get it to return the last submission but cant work out how to only return the current users last submission. I think it has something to do with get_currentuserinfo(); and AND user_id = %d", $form_id, $user_ID from what I can work out. I discovered get_currentuserinfo(); here: http://codex.wordpress.org/Function_Reference/get_currentuserinfo

You can see my coding below. Please let me know if you need any further information.

<?php 
global $wpdb, $ipt_fsqm_info, $user_ID;
get_currentuserinfo();
$form_id = 9;
$data_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id 
                                               FROM {$ipt_fsqm_info['data_table']}
                                              WHERE form_id = %d 
                                           ORDER BY id DESC LIMIT 0,1 
                                                AND user_id = %d", $form_id, $user_ID ) );

foreach ( $data_ids as $data_id ) {
    $data = new IPT_FSQM_Form_Elements_Data( $data_id );
    echo wpautop( $data->data->pinfo[14]['value'] );
}
?>

1条回答
女痞
2楼-- · 2019-09-07 20:36

You need to move the AND user_id = %d

$data_ids = $wpdb->get_col( $wpdb->prepare
( "SELECT id FROM {$ipt_fsqm_info['data_table']} 
WHERE form_id = %d AND user_id = %d 
ORDER BY id DESC LIMIT 0,1", $form_id, $user_ID ) );
查看更多
登录 后发表回答