搜索引擎:Php从定位标记获取数据

时间:2018-08-29 16:53:12

标签: php jquery html sql anchor

我在这里有这个html搜索:

      <form action="index.php" method="POST">
      <input id="search" type="text" placeholder="Search for Friends" name="search_name">
      <input class="submit" type="submit" name="search-submit" value="Search">
      </form>

此搜索引擎的php代码:

<?php
  if (isset($_POST['search-submit'])) {
   $search_name = $_POST['search_name'];
   $aVar = mysqli_connect('localhost','root','','socialnetwork');
   $result = mysqli_query($aVar, "SELECT username FROM users WHERE username LIKE '%$search_name%'");
   $found = 1;
   while ($row = mysqli_fetch_assoc($result)) {
     $username = $row['username'];
     @$output .= '<h2 class="friends-display"><a href="">'.$username.'</a></h2><hr>'; 
   }
 }

 ?>

现在此代码可以正常工作。它允许用户搜索其他用户。  定位标记“ friends-display” 通过使用变量$output显示代码的结果。 $output然后在后面回显。

我的问题如下:我想做一个if语句,以便当用户单击锚标记“ friends-display” 时,它应该显示该用户名的个人资料点击。

示例:您搜索 Mike ,并找到此用户名。比单击它,它应该显示 Mike 的个人资料。我该如何搭配锚标签?

我已经尝试过if isset(),但是对我来说不起作用。

1 个答案:

答案 0 :(得分:0)

按如下所示更改输出变量

....
while ($row = mysqli_fetch_assoc($result)) { 
    $username = $row['username'];
     @$output .= '<h2 class="friends-display"><a href="profile.php?user='.$username.'">'.$username.'</a></h2><hr>'; 
}
....

创建一个新的文件profile.php并添加以下基本行。

<?php
If(isset($_GET['user']) && !empty($_GET['user'])){
    $username = $_GET['user'];
    // check for username found in database. 
    // if not found exit with error "user not found"
    // else show user profile
}else{
    Die("unothrized access");
}

我希望这将指导您实现目标。 编码愉快:)