简体   繁体   中英

Retrieving row from table based on logged in user

I have a draggable div with position being saved to database. 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:

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(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'>";
}

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.

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