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

时间:2016-03-11 16:55:27

标签: php

下面是我编写的代码,它将显示来自phpmyadmin表的数据并显示结果。现在,一旦显示搜索结果,让我们说如果我希望点击其中一个搜索结果,例如我点击“Jon Doe”然后点击我应该被重定向到Jon Doe的个人资料页面(HTML页面)。我可以知道我该怎么做?

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

2 个答案:

答案 0 :(得分:0)

正如Funk Doc已经建议你应该制作一个“profile.php”,然后查询所有信息。

首先,您需要链接个人资料页面。您需要数据库中的“id”。

MySql.Execute(
    "mysql.yourhost.com", 
    "username", 
    "password", 
    "database", 
    "select * from Users", 
    function (data) {
        console.log(data)
});

现在,您将使用特殊ID将用户重定向到profile.php。

示例:profile.php?id = 87341

在你的profile.php中,你现在需要获取id变量。

<强> profile.php

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

ID现在保存在'$ userid'中。只需在数据库中搜索该ID,即可获得所有信息。

答案 1 :(得分:0)

好吧,假设你有2个用户John和Nick。 John的id = 1且Nick id = 2

<a href="handler.php?id=1">John</a>
<a href="handler.php?id=2">Nick</a>

<强> handler.php

<?php
$id=$_GET['id'];
$query='SELECT * FROM #__users where id='.$id;
//show your results

?>