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


  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;


    $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)
    x_pos = VALUES(x_pos), y_pos = VALUES(y_pos)",


Source: wpdb

Leave a Reply