简体   繁体   English

根据登录用户从表中检索行

[英]Retrieving row from table based on logged in user

I have a draggable div with position being saved to database. 我有一个可拖动的div,位置被保存到数据库中。 But when I try to query the database, it isn't echoing last position. 但是,当我尝试查询数据库时,它没有回显最后一个位置。 Is there something I am missing here? 我在这里缺少什么吗? Any input is greatly appreciated. 任何输入,不胜感激。

UPDATE: I have rewritten the code below and all is now working properly 更新:我已经重写了下面的代码,并且所有现在都可以正常工作

global $wpdb;

$user_exists = $wpdb->get_row($wpdb->prepare("SELECT * FROM coords WHERE user_id = %d", $current_user->ID));

if($user_exists) {
  echo "<div id='draggable' style='left:".$user_exists->x_pos."px; top:".$user_exists->y_pos."px;' class='flex_cell_inner'>";

}else{

  echo "<div id='draggable' class='flex_cell_inner'>";
    };

Here is the code I am using to save data to database which works.. but I am having problem with updating div position on frontend: 这是我用来将数据保存到数据库的代码,它可以工作..但是我在更新前端的div位置时遇到问题:

global $wpdb;

    $_POST['user_id'];
    $_POST['div_id'];
    $_POST['x_pos'];
    $_POST['y_pos'];

    $user_id    = $_POST['user_id'];
    $div_id     = $_POST['div_id'];
    $x_pos      = $_POST['x_pos'];
    $y_pos      = $_POST['y_pos'];

$wpdb->query($wpdb->prepare(" INSERT INTO coords

(user_id, div_id, x_pos, y_pos)
    VALUES (%d, %s, %d, %d)
    ON DUPLICATE KEY UPDATE
    x_pos = VALUES(x_pos), y_pos = VALUES(y_pos)",

    $user_id,
    $div_id,
    $x_pos,
    $y_pos
    ));

try this 尝试这个

function userIdExists($user_id){
    global $wpdb;
    $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users WHERE ID = %d",$user_id));
    if($count == 1){ return true; }else{ return false; }
}

Call this function with if condition 使用if条件调用此函数

if(userIdExists($user_id)){
     echo "<div id='draggable' style='left: ".$x_pos."px; top: ".$y_pos."px;' class='flex_cell_inner'>";
}else{
     echo "<div id='draggable' class='flex_cell_inner'>";
}

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

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