搜索结果直接搜索结果页

时间:2016-01-09 05:03:02

标签: php mysql

我正在从MYSQL进行简单搜索,它会从我的表中显示一个数据 " EMPLOYEE_ID"它已经有一个关于输出的链接我希望它指向具有相同id的另一个页面并从其被调用的id输出整个表

我已经有代码指向另一个页面但显示了我表格中的所有数据

我是php新手,我也是学生

形式:

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

搜索

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


?>

2 个答案:

答案 0 :(得分:1)

您需要解析要使用的ID:

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

然后在singlesearchresult.php中使用查询中的$_GET['id']

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

NB:这只是一个疯狂不安全的例子,在查询中使用它之前,你必须先清理网址中传递的值

答案 1 :(得分:0)

搜索

使用此:

#LoadModule rewrite_module modules/mod_rewrite.so

singlesearchresult 页面上:

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