nikesh
nikesh

Reputation: 140

sql retrieve data from table and make them links

I am generating web pages from database. Now my question is:

I have 1000 records(names) in my database(MySql). I have made a search box in a page and when i enter any name or a part of name that is in my DB all the name's should come up. Eg-

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

Now i want to show the selected names(fetched through the query) on the new page so that when i click on any of the name a new page should open up and all the data related to that selected name (present in the table belonging to the database)to be shown on that page with navigation buttons, what query should i use to perform it.

In short i want to make my page like Google search page.

My first page is like this

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

Second page is somewhat like this

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

Upvotes: 3

Views: 5118

Answers (2)

iOSBee
iOSBee

Reputation: 229

But the question is how will you make all the names as links and then fetch their result on next page .. right ?

Upvotes: 0

Rainer.R
Rainer.R

Reputation: 458

Well, simplified, on the first page you'll have something like:

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

and on the second page you have name, passed as URL parameter, on which you then do another database look up to get the contacts details and populate the various fields:

$_name = $GET['name'];

Please remember to add the required escapes or rather use PDO / mysqli

Upvotes: 1

Related Questions