doumengbai2031 2018-08-29 16:53
浏览 67
已采纳

搜索引擎:Php从anchor-tag获取数据

I have this html search here:

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

And this php code for the search engine:

<?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>'; 
   }
 }

 ?>

Now this code is working fine. It allows the user to search for other users. The anchor tag "friends-display" shows the result of the code by using the variable $output. The $output is then echoed later in the aside.

My problem is the following: I want to make an if-statement so when the anchor tag "friends-display" is clicked by the user it should show the profile of the username the user has clicked on.

Example: you search for Mike and you find this username. Than you click on it and it should show the profile of Mike. How can I make this with an anchor tag?

I have tried if isset(), but it did not work for me.

</div>
  • 写回答

1条回答 默认 最新

  • doulu8415 2018-08-29 17:18
    关注

    Change the output variable like below

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

    Create a new file profile.php and add the following basic line.

    <?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");
    }
    

    I hope this will guide you to achieve your target.. Happy coding :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址