douxihui8270 2016-03-11 16:55
浏览 27
已采纳

如何在PHP中将搜索结果与HTML页面链接

Below is the code that i have written that would display data from phpmyadmin table and display the result. Now once the search result is displayed, let's say if i wish to click on one of the search result for e.g. i clicked on "Jon Doe" then upon click i should be redirected to the profile page (HTML page) of Jon Doe. May i know how do i do that ?

<?php
 echo "<body style='background-color:gray'>";
include ("account.php");
( $dbh = mysql_connect( $hostname, $username, $password ))
    or die ( "uable to connect to MYSQL database" );
mysql_select_db( $project );


if (isset($_POST['search'])) {
    $sql= "SELECT * FROM registration ";

    $search_term = mysql_real_escape_string($_POST['search_box']);

    $sql .= "WHERE first_name= '{$search_term}'";

    $sql .= " OR last_name= '{$search_term}'";
    $query=mysql_query($sql) or die(mysql_error());
}



?>



<html>  
<head>  
<title>jon</title>  
</head>  
<body>  
<form name="search_form" method="POST" action="retrieve.php">  
<table width="599" border="1">  
<tr>  
<th>Search

<input type ="text" name ="search_box" value=""/>
<input type="submit" name="search" value="Find Users">

</tr>  
</table>  
</form>  


<table width="600" border="1">  
<tr>  
<th width="91"> <div align="center">First Name </div></th>  
<th width="98"> <div align="center">Last Name </div></th>  
<th width="198"> <div align="center">Email </div></th>  
<th width="97"> <div align="center">City </div></th>  
<th width="59"> <div align="center">Country </div></th>     

<tr>


<?php  if (isset($_POST['search'])) {
while ($row=mysql_fetch_array($query)){ ?>


<tr>
    <td><?php echo $row['first_name'];?></td>
    <td><?php echo $row['last_name'];?></td>
    <td><?php echo $row['email'];?></td>
    <td><?php echo $row['address_city'];?></td>
    <td><?php echo $row['address_country'];?></td>



<tr>

<?php }} ?>

</table>

</div>
  • 写回答

2条回答 默认 最新

  • dongzhanbi0027 2016-03-11 17:12
    关注

    As Funk Doc already suggested you should make a "profile.php" for example which then queries all informations.

    First you need to link the profile page. You need a "id" from your database.

     <td><a href="profile.php?id=<?php echo $row['id'];?>"><?php echo $row['first_name'];?></a></td>
    

    Now you will redirect your user to profile.php with a special id.

    example: profile.php?id=87341

    In your profile.php you now need to get the id variable.

    profile.php

    <?php
    $userid = $_GET['id'];
    ?>
    

    The id is now saved in ´$userid´. Just search your database for that id and you get all your informations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积