【问题标题】:Managing users view profiles system and passing the id to view profile page to display correct avatar image管理用户查看配置文件系统并传递 id 到查看配置文件页面以显示正确的头像图像
【发布时间】:2016-08-17 07:26:26
【问题描述】:

我正在尝试设置用户环境系统。我有 home.php 页面,这是一个个人资料页面和 planet_search.php,它显示所有能够查看其个人资料的用户。我正在努力将 id 变量传递给 home.php,所以我可以显示正确的头像。这里是 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: $(document).ready(function() {

$('.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']))

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

【问题讨论】:

    标签: php jquery ajax view profile


    【解决方案1】:

    尝试改用此代码。

    <?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;
        }
    });
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 具体在哪里?我将 common.php 包含到 session_start() 所在的所有文件中!
      • 当你想使用 $_SESSION 时,你需要先启动它,test1.php=> &lt;?php session_start(); $_SESSION["var1"]="hello"; ?&gt; test2php=> &lt;?php session_start(); echo $_SESSION["var1"];//hello ?&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2010-09-16
      • 2020-08-24
      相关资源
      最近更新 更多