PHP将会话用户数据转换为变量

时间:2018-04-19 15:57:20

标签: php mysql mysqli

我在为会话用户创建变量时遇到问题。我正在创建一个练习拳击网站,它将会话用户与具有相同属性的人匹配,即权重

<?php

session_start();
include "connection.php";

$id = $_GET["userID"];
$sql = "SELECT * FROM userdetails WHERE userID = '" . $id . "'";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo"<div>" .
        "<br>First name: " . $row["firstname"] .
        "<br>Second name: " . $row["secondname"] .
        "<br>Mobile: " . $row["mobile"] .
        "<br>Height: " . $row["height"] . "CM" .
        "<br>weight: " . $row["weight"] . "KG" .
        "<br>Image:<br> <img src=getimage.php? 
userID=" . $row["userID"] . "width=100, height=100" .
        "<br> You are a match! click below to view events" .
        "<br><a href=Events.php?userID=" . $row["userID"] . ">View 
Events</a>" .
        "</div>";
    }
} else {
    echo"0 results";
}
$weight = $row['weight'];
?>

此代码允许我收集并显示表格中个人的数据,$ weight = $ row ['weight']; line将个人的权重放入变量中。

我不确定如何将会话用户权重转换为变量,然后如何比较两者。我想我需要一份IF声明。类似的东西:

if ($weight == $sessionusersweight){
echo "you're a match";
}
else{
echo "you're not a match";
}

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:0)

我建议首先使用先打开:

<header>
    <link href="https://fonts.googleapis.com/css?family=Black+Han+Sans" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Gugi" rel="stylesheet">
</header>
<body class="boDy">
<div class="myDiv">
    <div class="bg"></div>
    <h1 class="myfont2 text-center"> <span style="color:purple">T.E.Q</span> 
    <span style="color:green">MOLECULE</span> MEDIA</h1>
</div>
<nav id="nav-example" class="navbar navbar-inverse bg-dark affix sticky" data-target-offset="18">  
    <a class="navbar-brand myfont" href="#">TEQ Molecule</a>
    <ul class="nav nav-pills">
        <li class="nav-item">
            <a class="nav-link" href="#Portfolio">Portfolio</a>
        </li>
        <li class="nav-item">
            <a class="nav-link " href="#About">About</a>
        </li>
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
            <div class="dropdown-menu">
                <a class="dropdown-item" href="#Contact">Contact</a>
        </li> 
        </div>
    </ul>
</nav>
<div class="bckground">
    <div class="bb1"></div>
    <br></br>
    <br></br>
    <br></br>
<div id="#scroller" data-spy="scroll" data-target="#nav-example" data-offset="10">
    <div class="myfont2" id="About" style="left:20px">
        <br></br>
        <h3>ABOUT T.E.Q</h3> 
    </div>
<img class="smallerImage"style="margin-left:20px"src="http://farm1.staticflickr.com/860/40814758834_31a56037fb_b.jpg" alt="about TEQ">
<br></br>
<br></br>

<div class="divider"></div>
<br></br>

<div id="Portfolio" class="myfont2">
    <br></br>
    <h3>PORTFOLIO</h3>
</div>
<br></br>
<div class="row">
    <div class="col-md-4">
        <a href="#"> <img class="thumbnail imgCenter" src="http://farm1.staticflickr.com/842/41527921141_e5c1bdbd42_b.jpg" alt="graphic arts"></a>
        <p class="text-center myfont"> GRAPHIC DESIGN</p>
    </div>
      <div class="col-md-4">
        <a href="#"> <img class="thumbnail imgCenter"  src="https://f9tech.com/wp-content/uploads/2018/02/dev-1.jpg" alt="web deveopment"></a>
        <p class="text-center myfont"> WEB DEVELOPMENT</p>
      </div>
      <div class="col-md-4">
        <a href="#"><img class="thumbnail imgCenter" align="center" src="https://scontent.fbgi2-1.fna.fbcdn.net/v/t1.0-9/25299007_1255958357881913_5312854462633629399_n.jpg?_nc_cat=0&oh=0c924733b42ea92b9916ab8576192094&oe=5B59AE75" alt="digital marketing"></a>
        <p class="text-center myfont">DIGITAL MARKETING</p>
    </div>
</div>
    <br></br>
    <div class="divider2"></div>
</div>
</div>
</body>

然后插入所需的所有代码,一旦完成,您需要的是将行的值赋给变量,这样您就可以执行此操作:

<?php
session_start()
?>

答案 1 :(得分:0)

因此,您的会话中有一位用户。如果是这样,那么您可以从$_SESSION变量中获取该用户。

要在会话中存储数据,您可以执行以下操作:

<?php
session_start();
$_SESSION['user'] = $row;

现在,您将$_SESSION变量中的行数据作为user索引。 要从$_SESSION变量中获取用户,您可以执行以下操作:

<?php 
session_start();
$sessionUser = $_SESSION['user'];

此处,session_start()方法用于启动会话。这在同一文件中不需要两次。

然后您可以根据需要进行检查:

<?php
if($row["weight"] == $sessionUser['weight'])
  echo "matched";
else
  echo "not matched";

答案 2 :(得分:0)

我已经让你成为我认为你想要的东西(这不是太明显)的版本。

<?php
session_start();
include "connection.php";

$id = $_GET["userID"]; // SANITISE USER INPUT!!!
        // change that to a prepared statement!! This is unsecure.
$sql = "SELECT * FROM userdetails WHERE userID = '" . $id . "'";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) == 1) {
    $row = mysqli_fetch_assoc($result); // no need for a while there, we expect only one row
    echo "<div>" .
        "<br>First name: " . $row["firstname"] .
        "<br>Second name: " . $row["secondname"] .
        "<br>Mobile: " . $row["mobile"] .
        "<br>Height: " . $row["height"] . "CM" .
        "<br>weight: " . $row["weight"] . "KG" .
        "<br>Image:<br> <img src=getimage.php? 
    userID=" . $row["userID"] . "width=100, height=100" .
        "<br> You are a match! click below to view events" .
        "<br><a href=Events.php?userID=" . $row["userID"] . ">View 
    Events</a>" .
        "</div>";

        // find users with the same weight;
        $findSimularity = 'weight'; // having weight as a var keeps it more modular/reuseable
        $stmt = mysqli_prepare($con, "SELECT * FROM userdetails WHERE `{$findSimularity}` = ?");

        mysqli_stmt_bind_param($stmt,"d", $row[$findSimularity]); // I assumed type double for weight here. 
                                                           //Could also be 'i' for int or 's' for a string/varchar. ?
        mysqli_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        echo "Similar Users:<br>";
        while($similarUser = mysqli_fetch_assoc($result)) {
            echo $similarUser['firstname'] . " has the same $findSimularity of ".$row[$findSimularity]."<br>";
        }


} elseif (mysqli_num_rows($result) > 1) {
    echo "I found more than one user with given ID. That should be an error.";
} else {
    echo "0 results";
}

// best practice is to NOT have a final ?> in your files (to avoid some strange hard to find errors), so I removed it