我正在从数据库生成网页。现在我的问题是:

我的数据库(MySql)中有1000条记录(名称)。
我在页面上做了一个搜索框,当我输入数据库中的任何名称或名称的一部分时,所有名称都应该出现。
例如-

SELECT * FROM table where name like '%$find%'

现在,我想在新页面上显示所选名称(通过查询获取),以便当我单击任何名称时,应打开一个新页面,并且与该所选名称相关的所有数据(在属于该表中数据库)显示在该页面上,并带有导航按钮,我应该使用哪种查询来执行该查询。

简而言之,我想让我的页面像Google搜索页面一样。

我的第一页就是这样
<html>
<body >
<h2>Search</h2>
<form name="search" method="post" action="second.php">
Search Name: <input type="text" name="find" id="find" />
<input type="submit" name="search" value="search" />
</form>
</body>
</html>

第二页有点像这样
  <html>
  <head>
  <script>
  function favBrowser()
  {
  var mylist=document.getElementById("opt");
  document.getElementById("favorite").value=mylist.options[mylist.selectedIndex].text;

   }
  </script>
  </head>

 <body>

  <form  method="get">

 <?php
 $find = $_REQUEST['find'];
 $con = mysql_connect("localhost","root","");
 if (!$con)
 {
 die('Could not connect: ' . mysql_error());
  }
 mysql_select_db("data", $con);


  $result = mysql_query("SELECT * FROM table where name like '%$find%'");


  $result_rows = mysql_num_rows($result);

 while($row = mysql_fetch_array($result))
{

 // $names[] = $row['name'];
 // echo $names[0];

 // echo "$row[name]. $row[id] <a href='data.php?edit=$row[name]'>edit</a><br />";

 $_name = $row['name'];

 echo "Name : <input type='text' name='name' value='$_name' size='30'>";

 echo "<br />";
 }
 }
 mysql_close($con);

?>

<!--</select>
<input type ="submit" value="submit">
<p>Your selected name is: <input type="hidden" name="fun" id="favorite" size="30">
</p>
 -->


</body>
</html>

最佳答案

好吧,简化了,在第一页上,您将看到以下内容:

while($row = mysql_fetch_array($result))
{
   $_name = $row['name'];
   echo '<a href="second_page.php?name='.strip_tags($_name)'" target="_BLANK"'.'</a>';
}

在第二页上,您具有作为URL参数传递的名称,然后在其上进行另一个数据库查找以获取联系人详细信息并填充各个字段:
$_name = $GET['name'];

请记住要添加所需的转义符,或者使用PDO / mysqli

关于php - sql从表中检索数据并使它们链接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13857310/

10-11 03:39
查看更多