我有一个可拖动的div,其位置被保存到数据库中。但是当我尝试查询数据库时,它并没有回应最后的位置。这里有什么我想念的吗?任何输入都非常感谢。
更新:我已经重写了下面的代码,现在一切正常了
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'>";
};
以下是我用于将数据保存到数据库的代码。但是我在前端更新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
));
答案 0 :(得分:0)
试试这个
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; }
}
使用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'>";
}