我如何通过员工ID从INNER JOIN表中搜索

时间:2014-03-27 16:56:21

标签: php mysql sql database

我创建了两个表 1:Employee_Personal_Info和2:补偿。我给了他们与EPI_NO(员工ID)的关系。当我实现此查询

$sql_search = "Select * FROM Employee_Personal_Info
INNER JOIN Compensation ON Employee_Personal_Info.EPI_NO=Compensation.EPI_NO;"
or die (sql_error);

它工作正常并显示所有记录。现在,我想通过提交EPI_NO id来搜索特定员工。

<form action="" method="get">
Search From Database <input type="text" name="searchdata"/> </br>
<input type="submit" value="Employee Personal and Contact Info" name="info" /> </form>

Plz帮帮我。

3 个答案:

答案 0 :(得分:0)

添加WHERE声明

$sql_search = "Select * FROM Employee_Personal_Info
INNER JOIN Compensation ON Employee_Personal_Info.EPI_NO=Compensation.EPI_NO
WHERE Employee_Personal_Info.EPI_NO = 1;"
or die (sql_error);

在这种情况下,1是您要查找的员工ID。

要将其与您的代码集成,您需要prepare您的查询。

这是一个例子,使用上述文档中的代码。

$id = $_POST['searchdata'];

/* create a prepared statement */
if ($stmt = $mysqli->prepare("Select * FROM Employee_Personal_Info
INNER JOIN Compensation ON Employee_Personal_Info.EPI_NO=Compensation.EPI_NO
WHERE Employee_Personal_Info.EPI_NO = ?")) {

    /* bind parameters for markers */
    $stmt->bind_param("s", $id);

    /* execute query */
    $stmt->execute();

    /* bind result variables */
    $stmt->bind_result($employee);

    /* fetch value */
    $stmt->fetch();

    /* close statement */
    $stmt->close();
}

答案 1 :(得分:0)

使用此,

$ sql_search =&#34;选择* FROM Employee_Personal_Info INNER JOIN薪酬在Employee_Personal_Info.EPI_NO = Compensation.EPI_NO AND Employee_Personal_Info.EPI_NO =&#34;。$ _ GET [&#39; searchdata&#39;]。&#34;;&#34;

答案 2 :(得分:0)

通过这种方式,您可以添加WHERE语句

<?

$sql_search = "Select * FROM Employee_Personal_Info
INNER JOIN Compensation ON Employee_Personal_Info.EPI_NO=Compensation.EPI_NO;" or die (sql_error);

if(isset($_POST['searchdata']))
{
  $sql_search = $sql_search."  WHERE ".$_POST['searchdata'];
}

?>

在这种情况下,您使用的是get in form,因此您可以使用$ _GET [&#39; searchdata n#39;]

在if子句中请求它