数据库中employee与tbl_feedback表合并及关联显示员工全名问题咨询
Hey there! Let's get this sorted so your employees see their full name automatically when accessing the feedback form. Here's a step-by-step solution tailored to your setup:
1. First: Pin Down the Common Link Between Your Tables
Since employee and tbl_feedback don't share the same ID, you need a unique, shared field to connect them. This could be an employee email, company ID number, or even a username—something that exists in both tables and uniquely identifies each employee. For this example, I'll assume you use employee_email as the linking field (swap this out for whatever matches your schema!).
2. SQL Query to Fetch the Full Name
To pull the employee's full name alongside their feedback data (or pre-load it for the form), use a SQL JOIN. Here are two common scenarios:
Scenario 1: Pre-fill the form for a logged-in employee
If the employee is already logged into your EIS, fetch their name directly from the employee table:
SELECT full_name FROM employee WHERE employee_email = '[logged-in-user-email]'; -- Replace with the current user's identifier
Scenario 2: Load existing feedback with the employee's name
If you're viewing past feedback entries, join both tables to get the name and feedback details together:
SELECT e.full_name, f.* FROM tbl_feedback f INNER JOIN employee e ON f.employee_email = e.employee_email -- Use your actual linking field here WHERE f.feedback_id = '[target-feedback-id]'; -- For a specific feedback entry
3. Backend Code Example (PHP/MySQL)
Here's a secure way to fetch the full name and pass it to your feedback form (using prepared statements to avoid SQL injection):
// Assuming you have an active PDO database connection and user session $loggedInEmail = $_SESSION['employee_email']; // Get the logged-in employee's email from session // Prepare and execute the query $stmt = $pdo->prepare("SELECT full_name FROM employee WHERE employee_email = ?"); $stmt->execute([$loggedInEmail]); $employee = $stmt->fetch(PDO::FETCH_ASSOC); $employeeFullName = $employee['full_name'] ?? 'Guest'; // Fallback if no name found
4. Frontend: Display the Full Name in the Form
On your Employee Feedback Form page, add a field to show the full name (set it to read-only so employees can't edit it):
<div class="form-section"> <label for="fullName">Your Full Name:</label> <input type="text" id="fullName" name="fullName" value="<?php echo htmlspecialchars($employeeFullName); ?>" readonly> </div>
5. Button Navigation Best Practice
When users click the Employee Feedbacks button, ensure the logged-in employee's identifier is passed to the form page. Using sessions is the safest way (no sensitive data in the URL):
<a href="employee-feedback-form.php" class="btn">Employee Feedbacks</a>
(If you're not using sessions, you could pass the identifier as a URL parameter, but sessions are more secure for authenticated users.)
Quick Tips
- Double-check your database schema to confirm the correct linking field between
employeeandtbl_feedback—this is critical for the join to work! - If you're using a framework like Laravel, Spring Boot, or Django, use the framework's ORM tools for joins instead of raw SQL (e.g., Laravel's
DB::table('tbl_feedback')->join('employee', ...)). - Always sanitize user input and use prepared statements to protect against SQL injection.
内容的提问来源于stack exchange,提问作者Renzo

