JustAngelo
JustAngelo

Reputation: 29

Search result direct to search result page

I'm making a simple search from MYSQL, it displays a single data from my table "employee_id" it already has an link on output i want it to direct to another page with the same id and output the whole table from its called id

I already have the code to direct to another page but is displays all the data from my table

I am new on php and I am also a student

form:

<form action="searchemp.php?searchresult" method="post"> 
    <p id="srchlbl">Search by Employee ID:</p>
    <input id="srchtxt" type="text" name="employee_id" placeholder="Search for Employee..."><br>
    <input id="srchbttn" name="search" type="submit" value="search">
    <input name="viewall" type="submit" value="view all">

search

if(isset($_POST['search']))
{   if(isset($_GET['searchresult']))
    {       if(preg_match("/^[  a-zA-Z]+/", $_POST['employee_id']))
        {   $name = $_POST['employee_id'];
            $sql = "SELECT  * FROM employee WHERE employee_id LIKE '%" . $name .  "%'"; 
            $result=mysql_query($sql);



            $row = mysql_fetch_array($result) or die($result."<br/><br/>".mysql_error());
            $e_id  = $row['employee_id']; 

            echo "<ul>\n"; 
            echo "<b>search results</b>";
            echo "<li>" . "<a  href=\"singlesearchresult.php?action=employee_id\">".$e_id . "</a></li>\n"; 
            echo "</ul>"; 
        } 
    } 
        else{ 
            echo  "<p>Please enter a search query</p>"; 
             } 
} 

singlesearchresult

<?php

$sql = 'SELECT employee_id, emp_fname, emp_lname, emp_address, emp_email, emp_contact FROM employee';
   $retval = mysql_query( $sql);

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
      echo      
                "<ul><li>EMPLOYEE ID : {$row['employee_id']}  <br> ".
                "EMPLOYEE NAME : {$row['emp_fname']} <br> ".
                "EMPLOYEE LASTNAME : {$row['emp_lname']} <br> ".
                "EMPLOYEE ADDRESS : {$row['emp_address']} <br> ".
                "EMPLOYEE EMAIL : {$row['emp_email']} <br> ".
                "EMPLOYEE CONACT : {$row['emp_contact']} <br> ".
                  "---------------------------------------------------------</li></ul>";

   }

   echo "<br><b>Fetched data successfully</b>\n";


?>

Upvotes: 0

Views: 95

Answers (2)

Pathik Vejani
Pathik Vejani

Reputation: 4491

On search

Use this:

echo "<li><a href='singlesearchresult.php?action=".$e_id."'>".$row['emp_fname']."</a></li>\n";

On singlesearchresult page:

<?php
$getEmpID = $_GET['action'];
$sql = "SELECT employee_id, emp_fname, emp_lname, emp_address, emp_email, emp_contact FROM employee WHERE employee_id = '$getEmpID' ";
$retval = mysql_query( $sql);
... // rest of your code
?>

Upvotes: 0

user557846
user557846

Reputation:

you need to parse the id to use:

 echo '<li><a  href="singlesearchresult.php?id='.$e_id.'">'.$e_id.'</a></li>\n"; 

then on singlesearchresult.php use $_GET['id'] in the query

$sql = 'SELECT employee_id, emp_fname, emp_lname, emp_address, emp_email, emp_contact FROM employee WHERE employee_id="'.$_GET['id'].'"';

NB: this is just an example its crazy unsafe, you must sanatise the value passed in the url before using it in the query

Upvotes: 1

Related Questions