Eiolon Posted October 20, 2007 Share Posted October 20, 2007 The information is displayed correctly. The problem comes into play with the record id's. The link I would click on for the employees name is echoing the job id and not the employee id so I get sent to the job page and not the employee profile. The link I would click on for the job is echoing fine. Thanks! I have tried various joins and I have tried employee.php?id=<?php echo $r_emp['e.id']; ?> instead of employee.php?id=<?php echo $r_emp['id']; ?> Any suggestions? <?php # department.php require_once('mysql_connect.php'); $q_emp = "SELECT e.id, e.first_name, e.last_name, e.job_id, j.id, j.title FROM employees e INNER JOIN jobs j ON (e.job_id = j.id) WHERE department_id = 4 ORDER BY last_name ASC"; $emp = mysql_query($q_emp) OR die ('Could not query employee information.'); $r_emp = mysql_fetch_array($emp); ?> <table width="100%" border="0" cellspacing="1" cellpadding="6" bgcolor="#CCCCCC"> <tr bgcolor="#E5E5E5"> <td><strong>Employee</strong> </td> <td><strong>Job Title</strong></td> </tr> <?php do { ?> <tr bgcolor="#FFFFFF"> <td><a href="employee.php?id=<?php echo $r_emp['id']; ?>"><?php echo $r_emp['last_name']; ?>, <?php echo $r_emp['first_name']; ?></a></td> <td><a href="job.php?id=<?php echo $r_emp['id']; ?>"><?php echo $r_emp['title']; ?></a></td> </tr> <?php } while ($r_emp = mysql_fetch_array($emp)); ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/74106-solved-having-problem-with-my-join-and-echoing-correct-id/ Share on other sites More sharing options...
esukf Posted October 20, 2007 Share Posted October 20, 2007 Isn't job id j.id? employee.php?id=<?php echo $r_emp['j.id']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74106-solved-having-problem-with-my-join-and-echoing-correct-id/#findComment-374167 Share on other sites More sharing options...
pocobueno1388 Posted October 20, 2007 Share Posted October 20, 2007 In your query, you are selecting two field names named "id". You need to alias those fields so the query will know which one to use. So do something like this SELECT e.id AS employeeID, e.first_name, e.last_name, e.job_id, j.id AS jobID, j.title FROM employees e INNER JOIN jobs j ON (e.job_id = j.id) WHERE department_id = 4 ORDER BY last_name ASC Now when you want to use e.id, do this $r_emp['employeeID'] If you want to use j.id, do this $r_emp['jobID'] Quote Link to comment https://forums.phpfreaks.com/topic/74106-solved-having-problem-with-my-join-and-echoing-correct-id/#findComment-374171 Share on other sites More sharing options...
Eiolon Posted October 20, 2007 Author Share Posted October 20, 2007 Thanks, I can't believe I overlooked aliasing. Works like a charm In your query, you are selecting two field names named "id". You need to alias those fields so the query will know which one to use. So do something like this SELECT e.id AS employeeID, e.first_name, e.last_name, e.job_id, j.id AS jobID, j.title FROM employees e INNER JOIN jobs j ON (e.job_id = j.id) WHERE department_id = 4 ORDER BY last_name ASC Now when you want to use e.id, do this $r_emp['employeeID'] If you want to use j.id, do this $r_emp['jobID'] Quote Link to comment https://forums.phpfreaks.com/topic/74106-solved-having-problem-with-my-join-and-echoing-correct-id/#findComment-374173 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.