Hi everyone,
I'm currently working on a project in Wappler where I need to save multiple days' worth of time tracking data to a MySQL database. The data is collected from a form that includes fields for each day of the month, and I need to store this data in a MySQL table.
Here's a brief overview of what I'm trying to achieve:
- Form Structure: I have an HTML form that collects time tracking data for each day of the month. This includes fields like date, client, job description, start time, end time, breaks, and special conditions for each day.
- MySQL Table: The data needs to be saved into a MySQL table named
time_tracking
with columns for each of the form fields. - Wappler Server Action: I need to create a Wappler Server Action that processes this form data and saves it to the MySQL table. This action should loop through each day of the month and insert the corresponding data into the database.
Steps to Achieve This:
1. Database Schema:
Here's the structure of my MySQL table:
CREATE TABLE time_tracking (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
date DATE,
client VARCHAR(255),
job_description VARCHAR(255),
shift_number VARCHAR(255),
state VARCHAR(255),
location VARCHAR(255),
start_time TIME,
end_time TIME,
break1_start TIME,
break1_end TIME,
break1_paid BOOLEAN,
break2_start TIME,
break2_end TIME,
break2_paid BOOLEAN,
vma BOOLEAN,
special_conditions VARCHAR(255),
working_time TIME,
additional_time TIME,
night_shift TIME,
sunday_shift TIME,
holiday_shift TIME,
total_working_time TIME,
total_gross DECIMAL(10,2)
);
2. Form Submission:
The form submits data for each day, and I need to ensure that the server action can handle and save all this data correctly.
Here's a snippet of the form HTML:
const daysInMonth = new Date(year, getMonthIndex(month) + 1, 0).getDate();
// Number of days in the month
const tableBody = document.getElementById('tableBody');
for (let day = 1; day <= daysInMonth; day++) {
const date = `${year}-${String(getMonthIndex(month) + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
// Formats the date
const weekday = new Date(date).toLocaleDateString('en-EN', { weekday: 'short' });
// Gets the weekday
tableBody.innerHTML += `
<tr>
<td>${day}.${month.slice(0, 3)}.${year}<br>- ${weekday}</td>
<td><input type="text" id="client_${day}" name="client_${day}" class="form-control" value="ERIXX SH"></td>
<td>
<select id="jobDescription_${day}" name="jobDescription_${day}" class="form-control" onchange="updateJobDescription(${day})" onchange="calculateTime(${day}, '${date}')" required>
<option value="">None</option>
<option value="Shift">Shift</option>
<option value="U">Vacation</option>
<option value="K">Sick</option>
<option value="Rest">Rest</option>
</select>
<input type="text" id="shiftNumber_${day}" name="shiftNumber_${day}" class="form-control d-none" placeholder="Shift No." onchange="calculateTime(${day}, '${date}')">
</td>
<td><input type="text" id="state_${day}" name="state_${day}" class="form-control" value="SH"></td>
<td><input type="text" id="location_${day}" name="location_${day}" class="form-control"></td>
<td><input type="time" id="start_${day}" name="start_${day}" class="form-control" onchange="calculateTime(${day}, '${date}')"></td>
<td><input type="time" id="end_${day}" name="end_${day}" class="form-control" onchange="calculateTime(${day}, '${date}')"></td>
<td><input type="time" id="break1From_${day}" name="break1From_${day}" class="form-control" onchange="calculateTime(${day}, '${date}')"></td>
<td><input type="time" id="break1To_${day}" name="break1To_${day}" class="form-control" onchange="calculateTime(${day}, '${date}')"></td>
<td><input type="checkbox" id="break1Paid_${day}" name="break1Paid_${day}" class="form-check-input" onchange="calculateTime(${day}, '${date}')"></td>
<td><input type="time" id="break2From_${day}" name="break2From_${day}" class="form-control" onchange="calculateTime(${day}, '${date}')"></td>
<td><input type="time" id="break2To_${day}" name="break2To_${day}" class="form-control" onchange="calculateTime(${day}, '${date}')"></td>
<td><input type="checkbox" id="break2Paid_${day}" name="break2Paid_${day}" class="form-check-input" onchange="calculateTime(${day}, '${date}')"></td>
<td><input type="checkbox" id="vma_${day}" name="vma_${day}" class="form-check-input" disabled></td>
<td>
<select name="specialConditions_${day}" id="specialConditions_${day}" class="form-control" onchange="calculateTime(${day}, '${date}')">
<option value="">None</option>
<option value="driverTraining">Driver Training</option>
</select>
</td>
<td><input type="text" id="workingTime_${day}" name="workingTime_${day}" class="form-control" readonly></td>
<td><input type="text" id="overtime_${day}" name="overtime_${day}" class="form-control" readonly></td>
<td><input type="text" id="night_${day}" name="night_${day}" class="form-control" readonly></td>
<td><input type="text" id="sunday_${day}" name="sunday_${day}" class="form-control" readonly></td>
<td><input type="text" id="holiday_${day}" name="holiday_${day}" class="form-control" readonly></td>
</tr>
`;
// Dynamically adds rows for each day of the month
}
Request for Help:
I would greatly appreciate any guidance or examples on how to create the Wappler Server Action to loop through each day's form data and insert it into the MySQL table. Specifically, I need help with:
- Setting up the server action to process multiple records.
- Handling potential errors during the data insertion.
- Ensuring data integrity and avoiding duplicate entries.
Thank you in advance for your assistance!