php - How to display foreign key data -
i have 3 table (leave, employee, department)
here database
employee table: { emp_id (pk), emp_fname, emp_lname, contactno_hp, contactno_home, emp_email, dept_id(fk)}
leave table: { leave_id (pk), date_apple, leave_type, leave_start, leave_end, status, emp_id(fk)}
department table: { dept_id (pk), dept_name, dept_desp}
when click "detail" come out new page. specific
<tr> <td><?php echo $row["leave_id"];?></td> <td><?php echo $row["emp_id"];?></td> <td><?php echo $row["date_apply"];?></td> <td><?php echo $row["leave_type"];?></td> <td><?php echo $row["leave_start"];?></td> <td><?php echo $row["leave_end"];?></td> <td><?php echo $row["status"];?></td> <td><a href="app_status.php?id=<?php echo $row[leave_id];?>" target="_blank">detail</a></td> </tr>
in new page having problem display (emp_name, dept_name, contactno_hp, contactno_home) emp_id foreign key in leave table , dept_name in department table link dept_id foreign key in employee table.
i use following statement in new page display data
<?php $result = mysql_query("select * `leave`"); $row = mysql_fetch_assoc($result); ?>
first of have retrieve leave_id in details page:
$leaveid = $_get['leave_id'];
then have write sql code select 3 tables , assign variable (like $sql):
select t1.emp_name, t2.dept_name, t1.contactno_hp, t1.contactno_home employee t1 inner join department t2 on t1.dept_id = t2.dept_id inner join `leave` t3 on t3.emp_id = t1.emp_id t3.leave_id = ?
at point suggest use mysqli object prepared statement:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); $stmt = $mysqli->prepare($sql); $stmt->bind_param("i", $leaveid); $stmt->execute(); $stmt->bind_result($empname, $deptname, $contactnohp, $contactnohome); while ($stmt->fetch()) { \\your code display details here } $stmt->close(); $mysqli->close();
please note that:
- "leave" reserved word, have enclose between quotes in sql statement;
- you have emp_id in page, should pass instead of leave_id; writing cleaner sql statement.
Comments
Post a Comment