sql从表中检索数据并使它们成为链接

时间:2012-12-13 10:00:06

标签: php javascript mysql sql database

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

我的数据库(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>

2 个答案:

答案 0 :(得分:1)

嗯,简化,在第一页上你会有类似的东西:

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

答案 1 :(得分:0)

但问题是如何将所有名称作为链接,然后在下一页上获取结果..对吗?