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

drkbr07n  于 2021-06-24  发布在  Mysql
关注(0)|答案(3)|浏览(287)

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

<?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'];行将个体的权重放入变量中。
我不确定如何将会话用户的权重放入一个变量中,然后如何比较两者。我想我需要一个if语句。比如:

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

任何帮助都将不胜感激

axzmvihb

axzmvihb1#

我建议的第一件事是首先使用打开:

<?php
session_start()
?>

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

$_SESSION = $row['weight'];
2nbm6dog

2nbm6dog2#

因此,会话中有一个用户。如果是这样,那么您可以从 $_SESSION 变量。
要在会话中存储数据,可以执行以下操作:

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

现在,您的行数据在 $_SESSION 变量as user 索引。从中获取用户 $_SESSION 您可以按以下步骤操作:

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

在这里, session_start() 方法用于启动会话。在同一个文件中不需要两次。
然后您可以随意查看:

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

neskvpey3#

我给你做了一个版本,我认为这是你想要(这不是太明显)实现的。

<?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

相关问题