I need some suggestions/help with my code. Code:
<form method='post' action='' autocomplete="off"> Start Date <input type='text' class='dateFilter' name='fromDate' value='<?php if(isset($_POST['fromDate'])) echo $_POST['fromDate']; ?>'> End Date <input type='text' class='dateFilter' name='endDate' value='<?php if(isset($_POST['endDate'])) echo $_POST['endDate']; ?>'> <input type='submit' name='but_search' value='Search'> </form>
<table border='1' width='100%' style='border-collapse: collapse;margin-top: 20px;'> <tr> <th>EmployeeName</th> <th>WorkDate</th> <th>WorkType</th> <th>CustomerName</th> </tr> <?php $emp_query = "SELECT * FROM employees WHERE 1"; // Date filter if(isset($_POST['but_search'])){ $fromDate = $_POST['fromDate']; $endDate = $_POST['endDate']; if(!empty($fromDate) && !empty($endDate)){ $emp_query .= " and workdate between '".$fromDate."' and '".$endDate."' "; } } // Sort $emp_query .= " ORDER BY id ASC"; $employeesRecords = mysqli_query($conn,$emp_query); // Check records found or not if(mysqli_num_rows($employeesRecords) > 0){ while($empRecord = mysqli_fetch_assoc($employeesRecords)){ $id = $empRecord['id']; $empName = $empRecord['username']; $date_of_join = $empRecord['workdate']; $gender = $empRecord['worktype']; $email = $empRecord['customername']; echo "<tr>"; echo "<td>". $empName ."</td>"; echo "<td>". $date_of_join ."</td>"; echo "<td>". $gender ."</td>"; echo "<td>". $email ."</td>"; echo "</tr>"; } }else{ echo "<tr>"; echo "<td colspan='4'>No record found.</td>"; echo "</tr>"; } ?> </table> </div>
When i select dateFrom and dateTo, everything works like a charm and form shows me records from db between two dates, but what i need: I want to display data for specific user. Example: I choose Employee Name > Micheal from and form shows me data for Micheal only, when i choose David, shows only for David not all employees. How can i do this?
I have database with 3 tables: employees, accounts and customers.
Under employees i have columns: id, username, workdate, startdate and enddate.
Advertisement
Answer
First: You should add a select option to your form, and on each row set the id as value.
Second: You need to get the id with: $id = $_POST[‘id’];
Third: Finally, you need to change your query to:
SELECT * FROM employees WHERE workdate BETWEEN ‘”.$fromDate.”‘ AND'”.$endDate.”‘ AND id = $id