Inserting multiple values ​​from multiple forms at once in PHP and MySQL

I am creating a system for my dissertation for school use. There is one aspect I cannot work with. I want to be able to set the attendance for several people at the same time. There will be an image that will show you what the form looks like:

multiple attendance form

All values ​​matter are present , so only a couple can be debugged to missing if needed. Once the button is clicked at the bottom of the form, I would like it to go to the confirmation page. I used a MySQl query to get a list of employees whose attendance has not yet been established and use an include tag to put this in an HTML form. The code I have to create looks like this:

<?php
// Get a list of all items and display them in ID order
$dynamicList = "";
$sql = mysql_query("SELECT StaffID, StaffName FROM StaffDetails WHERE StaffID NOT IN (SELECT StaffID FROM StaffAttendance WHERE AttendanceDate = curdate()) ORDER BY StaffID ASC");

// Show list
$productCount = mysql_num_rows($sql);
$setTodaysAttendanceList = "";
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)) {
    $StaffID = $row["StaffID"];
    $StaffName = $row["StaffName"];
    $setTodaysAttendanceList .= '<tr style="font-size:15px;">
  <td><a href="../staff_member_details.php?id=' . $StaffID . '">' . $StaffID . '</a></td>
  <td><a href="../staff_member_details.php?id=' . $StaffID . '">' . $StaffName . '</a></td>
  <td><label>
<select name="attendance_status" id="attendance_status">
<option value="Present">Present</option>
<option value="Absent">Absent</option>
</select>
</label></td>
  <td><label>
<textarea cols="21" rows="5" name="notes" id="notes" placeholder="Enter notes here..."></textarea>
</label></td>
</tr>';
}
} else {
$setTodaysAttendanceList = "There are no records listed at this time";
}
mysql_close();
?>

      

Then, in HTML, I have this:

<form action="set_multiple_staff_attendance_confirm.php" enctype="multipart/form-data" name="StaffAttendanceForm" id="StaffAttendanceForm" method="post">
    <?php echo $setTodaysAttendanceList; ?>
    <tr style="font-size:15px;">
      <td></td>
      <td></td>
      <td></td>
      <td><label>
      <input type="submit" name="addNewRow" id="addNewRow" value="Add Staff Attendance Records" />
      </label></form></td>
    </tr>

      

When it redirects to the next page, I have an insert request that looks like this:

<?php
// Add row to database
if (isset($_POST['staff_id'])) {
$staff_id = mysql_real_escape_string($_POST['staff_id']);
$attendance_status = mysql_real_escape_string($_POST['attendance_status']);
$notes = mysql_real_escape_string($_POST['notes']);

$sql .= mysql_query("INSERT INTO StaffAttendance (StaffID, AttendanceDate, AttendanceStatus, Notes) VALUES
('$staff_id', now(), '$attendance_status', '$notes')") or die (mysql_error());
$editid = mysql_insert_id();
}
?>

      

I know this is a long and confusing way to ask, but I'm just showing that I got it right and I'm completely stuck!

+3


source to share


1 answer


It looks like you are checking $_POST["staffid"]

, but in your menus select name="attendance_status"

.

You should set the names of your favorite menus as follows:

name="attendance_status[]"

      

Then in your php script, you read an array - not a single value - when you do $_POST["attendance_staff"]

.

EDIT: This will only work if you also have an array available with all relevant staff members. You can get this by using hidden input for each line whose name staffid[]

and whose value is state.

However, a more reliable way would probably be to use staffid inside the name of each element of the saved form, as David-SkyMesh pointed out in the comment on your post.



EDIT: For example, you can use this code to label your form elements (unchecked):

<select name="$staffID_attendance_status" id="$staffID_attendance_status">
    <option value="Present">Present</option>
    <option value="Absent">Absent</option>
</select>
<textarea cols="21" rows="5" name="$staffID_notes" id="$staffID_notes" placeholder="Enter notes here..."></textarea>
<input type='hidden' name='staffID[]' value='$staffID'>

      

Note that this will also give all of your form elements the unique IDs that are required if you want your HTML to be validated.

Then in your script, you can do this:

foreach ($_POST["staffID"] as $staffID) {
    $staff_id = mysql_real_escape_string($staffID);
    $attendance_status = mysql_real_escape_string($_POST[$staff_id . "_attendance_status"]);
    $notes = mysql_real_escape_string($_POST[$staff_id . "_notes"]);

    $sql .= mysql_query("INSERT INTO StaffAttendance (StaffID, AttendanceDate, AttendanceStatus, Notes) VALUES ('$staff_id', now(), '$attendance_status', '$notes')") or die (mysql_error());
    $editid = mysql_insert_id();
}

      

+4


source







All Articles