Php 检查mysql中是否存在用户id

Php 检查mysql中是否存在用户id,php,mysql,Php,Mysql,我在变量$\u SESSION['user\u ID']中存储了一个用户ID。要编辑帖子,此ID应与mysql表中的“user”字段匹配 如何设置语句来检查这些变量是否匹配 $query=“从用户为“”的tablename中选择*”$使用者。” 这将获得id为$id的帖子,并由用户id存储在$_会话['User_id']中的用户发布 由于如果用户试图编辑自己的帖子以外的帖子,您希望显示一条错误消息,因此您应该通过$id获取帖子详细信息,然后检查“用户”字段,使其与当前会话中的用户id匹配 $qu

我在变量$\u SESSION['user\u ID']中存储了一个用户ID。要编辑帖子,此ID应与mysql表中的“user”字段匹配

如何设置语句来检查这些变量是否匹配

$query=“从用户为“”的tablename中选择*”$使用者。”

这将获得id为$id的帖子,并由用户id存储在$_会话['User_id']中的用户发布

由于如果用户试图编辑自己的帖子以外的帖子,您希望显示一条错误消息,因此您应该通过$id获取帖子详细信息,然后检查“用户”字段,使其与当前会话中的用户id匹配

$query = "SELECT  * FROM table_name WHERE id = $id";
$rs    = mysql_query($query);
if(mysql_num_rows($rs)>0){
// post exists 
    while($row = mysql_fetch_assoc($rs)){
        if($row['user']===$_SESSION['user_id']){
            // allow the edit.
        }else{
            // show error user is not authorized to do the edits
        }
    }else{
        // show the error this is not a valid id; no posts exists with the given id
    }
}

$\u GET
$\u POST
。是否正确?$\u GET从url获取id,edit.php?id=4有什么问题?当你发布表单时,id仍然保留在url中?是的,我想实现的是,用户只能编辑自己的帖子。所以,如果在mysql和$\u会话[user\u id]中user都等于1,则继续编辑。否则,显示一个错误。
$query = "SELECT  * FROM table_name WHERE id = $id AND user = $_SESSION['user_id'];
$query = "SELECT  * FROM table_name WHERE id = $id";
$rs    = mysql_query($query);
if(mysql_num_rows($rs)>0){
// post exists 
    while($row = mysql_fetch_assoc($rs)){
        if($row['user']===$_SESSION['user_id']){
            // allow the edit.
        }else{
            // show error user is not authorized to do the edits
        }
    }else{
        // show the error this is not a valid id; no posts exists with the given id
    }
}