管理用户查看个人资料系统并将 id 传递到查看个人资料页面以显示正确的头像图像

Managing users view profiles system and passing the id to view profile page to display correct avatar image

我正在尝试设置用户环境系统。我有 home.php 页面,它是个人资料页面,planet_search.php 显示所有能够查看其个人资料的用户。我正在努力将 id 变量传递给 home.php,因此我可以显示正确的头像 image.Here home.php 中显示头像图像的部分:

<img class='ima'src="<?php 
                  $id = $_SESSION['id']; 
                  $query = "SELECT avatar_url FROM users WHERE id = '$id' LIMIT 1";
                  if(isset($_POST['id'])){$idu = $_POST['id'];
                    "SELECT avatar_url FROM users WHERE id = '$idu' LIMIT 1";}
                  $result = mysqli_query($conn,$query);

                  $row = mysqli_fetch_assoc($result);

                  if(!$row["avatar_url"]){echo 'img/profile3.png';}else{echo  $row["avatar_url"];}

              ?>" alt="face"  >

这是来自 planet_search.php 的 JQuery: $(文档).ready(函数() {

$('.square').on('click', function(){
  $this_id = $(this).find('.id_user').text();

  $.ajax({
    type: "POST",

    url: "home.php",

    data: { id: $this_id }, // or the string: 'id=1'
    complete:
    function () {
        window.location = "home.php";
    }

   });
   })
   });

但是它不起作用?它没有传递变量,因为

if(isset($_POST['id']))

永远不会是真的。我做错了什么?

你没有开始会话,session_start();

尝试改用此代码。

<?php 
$idu = null;

if (isset($_GET['id'])) {
    $idu = $_GET['id'];
} elseif (isset($_SESSION['id'])) {
    $idu = $_SESSION['id'];
}

$avatar_url = "img/profile3.png";

if (!is_null($idu)) {
    $query = "SELECT avatar_url FROM users WHERE id = '$idu' LIMIT 1";

    $result = mysqli_query($conn, $query);

    $row = mysqli_fetch_assoc($result);

    if ($row["avatar_url"]) {
        $avatar_url = $row["avatar_url"];
    }
}

?>

<img class='ima' src="<?php echo $avatar_url; ?>" alt="face">

和 JS:

$('.square').on('click', function () {
    $this_id = $(this).find('.id_user').text();

    if ($this_id != "") {
        window.location = "home.php?id=" + $this_id;
    }
});