链接到特定用户配置文件php

时间:2016-04-19 06:15:56

标签: php mysql database

我正在我的网站上创建搜索栏功能,用户可以使用名称搜索用户。搜索结果可能会出现多个名称相似的用户(例如,如果我搜索“Jenna”,我的数据库可能会有多个用户名为“Jenna”,因此会显示多个结果。)我希望用户能够点击其中一个配置文件并查看特定的“Jenna”用户配置文件。有点像Twitter,在那里我可以搜索帐户并查看不同的个人资料。现在我有代码返回搜索,并使搜索结果成为可点击的链接。但是,当我尝试保存用户ID时,它只保存最新的用户ID。

home.php(用户的搜索栏为0

<form  method="GET" action="search.php"  id="searchform">
    Search for users:
    <input  type="text" name="search_user" placeholder="Enter username">
    <input  type="submit" name="submit" value="Search">
</form>

search.php(打印出用户正在搜索的名称的用户)

session_start();
$user = '';
$password = '';
$db = 'userAccounts';
$host = 'localhost';
$port = 3306;
$link = mysqli_connect($host, $user, $password, $db);
mysqli_query($link,"GRANT ALL ON comment_schema TO 'oviya'@'localhost'");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$search_user = $_GET['search_user'];
$sql = "SELECT * FROM users WHERE username LIKE '%$search_user%'";
$result = mysqli_query($link, $sql);
if(mysqli_num_rows($result)>0){
    while ($row = mysqli_fetch_assoc($result)) {
        $a = '<a';
        $b = ' href="';
        $c = 'user_profiles.php';
        $d = '">';
        $e = $row['username'];
        $f = '</a';
        $g = '>';
        $_SESSION['user'] = $row['user_id'];
        $userID = $_SESSION['user'];
        echo $a.$b.$c.$d.$e.$f.$g;
        header("Location: user_profiles.php");
    }
}

user_profiles.php(根据用户点击特定用户ID的链接,显示特定用户的个人资料的显示位置)

session_start();
$userID=$_SESSION['user'];
$link = mysqli_connect('localhost', 'x', '', 'userAccounts');
$query="SELECT * FROM dataTable WHERE user_id='$userID'";
$results = mysqli_query($link,$query);
while ($row = mysqli_fetch_assoc($results)) {
    echo '<div class="output" >';
    $entry_id = $row["entry_id"];
    $output= $row["activity"];
    echo "Activity: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
    $output= $row["duration"];
    echo "Duration: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')." hrs"."<br>"."<br>";
    $output= $row["date_"];
    echo "Date: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
    echo '</div>';
}

我得到了我的错误,search.php中的while循环将只保存最新的userID,因此该链接将始终将我带到具有该useriD的用户配置文件。我只是不确定如何实现它,以便当用户查看配置文件列表时,他们单击的链接将根据用户ID将它们带到特定的配置文件。

3 个答案:

答案 0 :(得分:1)

您需要对search和user.php文件进行更改:

Search.php:

<?php
session_start();
$user = '';
$password = '';
$db = 'userAccounts';
$host = 'localhost';
$port = 3306;

$link = mysqli_connect($host, $user, $password, $db);
mysqli_query($link, "GRANT ALL ON comment_schema TO 'oviya'@'localhost'");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$search_user = $_GET['search_user'];

$sql = "SELECT * FROM users WHERE username LIKE '%$search_user%'";
$result = mysqli_query($link, $sql);

if (mysqli_num_rows($result) > 0) {

    while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['user_id'];
        ?>
        <a href="user_profiles.php?id=<?php echo $id; ?>" >
            <?php echo $row['username']; ?>
        </a>
        <?php
        $_SESSION['user'] = $row['user_id'];
        $userID = $_SESSION['user'];

        header("Location: user_profiles.php");
    }
}

<强> User_profile.php:

$userid = $_GET['id'];

$link = mysqli_connect('localhost', 'x', '', 'userAccounts');
$query = "SELECT * FROM dataTable WHERE user_id='$userid'";
$results = mysqli_query($link, $query);

while ($row = mysqli_fetch_assoc($results)) {
    echo '<div class="output" >';
    $entry_id = $row["entry_id"];
    $output = $row["activity"];
    echo "Activity: ";
    echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8') . "<br>" . "<br>";
    $output = $row["duration"];
    echo "Duration: ";
    echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8') . " hrs" . "<br>" . "<br>";
    $output = $row["date_"];
    echo "Date: ";
    echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8') . "<br>" . "<br>";
    echo '</div>';
}

答案 1 :(得分:0)

首先,您要将多个用户ID保存到字符串中。

另一件事,你是在循环中保存它。

因此,最新值会更新旧值。

在您的情况下,它将始终保存最后一个值。这是首要问题。

您可以使用一组用户ID并将其保存在其中。

$userIds = array();
while ($row = mysqli_fetch_assoc($result)) {
        $a = '<a';
        $b = ' href="';
        $c = 'user_profiles.php';
        $d = '">';
        $e = $row['username'];
        $f = '</a';
        $g = '>';
        $userIds[] = $row['user_id'];
        $userID = $_SESSION['user'];
        echo $a.$b.$c.$d.$e.$f.$g;
        header("Location: user_profiles.php");
    }
$_SESSION['user'] = $userIds;

user_profiles.php中,循环遍历数组或使用MySQL IN()条件获取所有用户个人资料。

另外,为什么你为html链接带来了太多的变量。您可以使用如下所示的连接在单个变量中执行此操作:

$userIds = array();
while ($row = mysqli_fetch_assoc($result)) {
 $a = '<a'
 . ' href="';
 . 'user_profiles.php';
 . '">';
 . $row['username'];
 . '</a';
 . '>';
 $userIds[] = $row['user_id'];
 $userID = $_SESSION['user'];
 echo $a;
 header("Location: user_profiles.php");
}
$_SESSION['user'] = $userIds;

另一个错误是您echo HTML链接并进行重定向。

这会导致headers already sent...错误。

答案 2 :(得分:0)

这将显示具有搜索字符串的用户列表

if(mysqli_num_rows($result)>0){

    while ($row = mysqli_fetch_assoc($result)) {
$link="<a href='user_profiles.php?user_id=".$row['user_id']."'>".$row['username']."</a>";
    }
}

点击链接后,它将重定向到user_profiles.php(不需要标题。标题用于自动重定向) 在user_profiles.php中

session_start();
$userID=$_GET['user_id'];

$link = mysqli_connect('localhost', 'x', '', 'userAccounts');
$query="SELECT * FROM dataTable WHERE user_id='$userID'";
$results = mysqli_query($link,$query);

while ($row = mysqli_fetch_assoc($results)) {
    echo '<div class="output" >';
    $entry_id = $row["entry_id"];
    $output= $row["activity"];
    echo "Activity: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
    $output= $row["duration"];
    echo "Duration: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')." hrs"."<br>"."<br>";
    $output= $row["date_"];
    echo "Date: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
    echo '</div>';
}