简体   繁体   English

PHP选择一个结果

[英]PHP Selecting a single result

I created a PHP form which allows users to Register and Log in . 我创建了一个PHP表单,允许用户注册和登录 Now I created another page named View.php that will show all the registered users in my MySQL database. 现在,我创建了另一个名为View.php的页面,该页面将显示 MySQL数据库中的所有注册用户 The code I used was 我使用的代码是

while($row=mysqli_fetch_assoc($sql))...

and it displayed all the users successfully . 并且成功显示了所有用户

Now I created another PHP page which I named profile.php . 现在,我创建了另一个名为profile.php的 PHP页面。 I want to add a link from every result on view.php which will redirect to profile.php?user=(their username) . 我想从view.php的每个结果中添加一个链接 ,该链接将重定向到profile.php?user =(其用户名) But I don't know how. 但是我不知道如何。

In your view.php considering that you have a column named 'username' , change the following : please not, it's preferably to put the ID column If you want to put the id column, simply change the $row['username'] to $row['id'] and the same in the query in profile.php 考虑到您有一个名为'username'的列,请在view.php中更改以下内容:请不要,最好放置ID列。如果要放置id列,只需将$row['username']更改为$row['id']profile.php中的查询中的相同

<?php
...
            while($row=mysqli_fetch_assoc($result)) {
                    echo "---------------------<br/>";
                    echo "<b>".$row['fullname']."</b><br/>";
                    echo "<small><i>".$row['course']."</i></small><br/>";
                    echo "<small><a href = 'friends.php?user=".$row['username']."'>[View Profile]</a></small><br/>";
                    echo "---------------------<br/><br/>";

            }

    ?>

And in your profile.php 并在您的profile.php中

<?php session_start();

        if($_SESSION['logged_in']==false) {
                header("Location:login.php");
        }


        include("header.php");

?>

<html>
<head>
        <title>View School-Mates</title>
</head>
        <body>
                <center>
                        <h1>My School-Mates</h1>
                        <small>View or Add them in your Trust List</small>
                        <br/><br/>
                        <hr>
                </center>

<?php
 try {
        $dbh = new PDO('mysql:host=localhost;dbname=test_basic', "root", "");

        $stmt = $dbh->prepare("SELECT * FROM USERS WHERE username= ?");
        if ($stmt->execute(array($_GET['user']))) {
            while ($row = $stmt->fetch()) {
                //here you will have your row with all your username data
            }
        }

} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>

</body>
</html>

Please read more about PDO from here and how to do connections this is required because you get data from your $_GET variable, and thus you need to avoid for sql injection 从此处阅读有关PDO的更多信息以及如何进行连接,这是必需的,因为您是从$ _GET变量中获取数据的,因此需要避免进行sql注入

Hopefully this is what you wanted, if not, please let me know so i can adjust the code 希望这是您想要的,如果不需要,请告诉我,以便我可以调整代码

I do not know the code you are using to achieve the result but having something like : 我不知道您用来实现结果的代码,但是有类似以下内容的代码:

$query = "SELECT * FROM database WHERE id=$id";
$query = mysql_query($query);

This will filter out the profile page according to the user id 这将根据用户ID过滤掉个人资料页面

In this line: 在这一行:

echo "<small><a href = 'profile.php?user=$them'>[View Profile]</a></small><br/>";

instead of using your fixed $them, just use $row['id']. 而不是使用固定的$ them,只需使用$ row ['id']。 Then you can fetch the user with that id in your profile.php file: 然后,您可以在profile.php文件中获取具有该ID的用户:

$id = $_GET['user'];
$sql = "SELECT * FROM users where id = $id";

Note that this code is prone to sql injection. 请注意,此代码易于进行sql注入。 I only posted it to make the idea easier to understand. 我只是发布它以使该想法更容易理解。 See here how to do it right. 看到这里如何正确做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM