Hi All,
So I spend quite a bit of time to get my form to create my radio buttons from a MySQL Table.
To give some background, the form has a drop-down menu with a number of options, these options refer to specific shift times / slots. On selecting an option from the drop-down, a number of rows are returned each time. Each row represents an agent, each agent then has 4 radio buttons namely Present (1), Late (2), Absent (3), AWOL (4). The value in the brackets represents the value that will be written to the table.
The problem I have is to be able to submit multiple rows / agents statuses at a time. The other form fields are Shift, Comments, Username (user completing form). These 3 fields will have the same data for each row being submitted.
Below is a sample of the code that creates the radio buttons:
<?php
$irow=0;
while($row = $result->fetch_assoc())
{
if($row['agent']==1)
$checked=" checked='checked'";
else
$checked='';
if($row['agent']==1)
$checked2=" checked='checked'";
else
$checked2='';
if($row['agent']==1)
$checked3=" checked='checked'";
else
$checked3='';
if($row['agent']==1)
$checked4=" checked='checked'";
else
$checked4='';
echo "<table><tr>";
echo "<td><label>".$row['agent'].":";
echo "<span class='small2'>Agent's attendance status</span>";
echo "</td>";
echo "<td><input type='radio' name='".$row['agent']."' value='1'".$checked."/>" ;
echo "<span class='small'>Present</span>";
echo "</td>";
echo "<td><input type='radio' name='".$row['agent']."' value='2'".$checked2."/>" ;
echo "<span class='small'>Late</span>";
echo "</td>";
echo "<td><input type='radio' name='".$row['agent']."' value='3'".$checked3."/>" ;
echo "<span class='small'>Absent</span>";
echo "</td>";
echo "<td><input type='radio' name='".$row['agent']."' value='4'".$checked4."/>" ;
echo "<span class='small'>AWOL</span>";
echo "</label></td>";
echo "</tr>";
$irow++;
}
echo "</tr></table>";
?>
The next section is the current submit page that I created, the problem being that it is attempting to insert everything into one row.
<?php
include_once '../includes/db_connect.php';
include_once '../includes/functions.php';
include_once '../includes/formatting.php';
/* USED FOR ERROR CHECKING */
/*
var_dump($_SESSION); exit;
var_dump($_POST);exit;
*/
if (isset(
$_POST['username']
, $_POST['shift']
, $_POST['agent']
, $_POST['agent_comments']))
{
// SANITIZE AND VALIDATE THE DATA BEING PROCESSED BY THE FORM
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$shift = filter_input(INPUT_POST, 'shift', FILTER_SANITIZE_STRING);
$agent = filter_input(INPUT_POST, 'agent', FILTER_SANITIZE_STRING);
$agent_comments = filter_input(INPUT_POST, 'agent_comments', FILTER_SANITIZE_STRING);
if (mysqli_connect_errno())
{
printf("Connect failed: %s\
", mysqli_connect_error());
exit();
}
if (empty($error_msg))
{
// INSERT THE NEW FOR INFORMATION INTO THE DATABASE TABLE
if ($insert_stmt = $mysqli->prepare("
INSERT INTO
usr_agent_shift
(
username
, shift
, agent
, agent_comments
)
VALUES (?, ?, ?, ?)"))
{
$insert_stmt->bind_param(
'ssss'
, $username
, $shift
, $agent
, $agent_comments
);
// EXECUTE THE PREPARED QUERY
if (! $insert_stmt->execute())
//PRINT THE NUMBERS OF ROWS THAT HAVE BEEN AFFECTED
{
//var_dump($_POST);exit;
header('Location: ../error.php?err=Registration failure: INSERT');
exit;
}
include "../success/agent_analysis_success.php";
exit;
}
/* CLOSE THE STATEMENT */
$stmt->close();
/* CLOSE THE CONNECTION */
$mysqli->close();
}
}
?>
This is what the array looks like that is being submitted:
array(6) { ["username"]=> string(13) "SalientAnimal" ["shift"]=> string(1) "7" ["Agent_1"]=> string(1) "2" ["Agent_2"]=> string(1) "1" ["Agent_3"]=> string(1) "2" ["agent_comments"]=> string(23) "Sample of current Array" }
How do I get it to store each result into a seperate array, and then to write each array to a new row in the table?
Each of these then needs to be added to a new row in the table. I am not sure if someone could maybe also suggest a better way of storing the 3 generic fields, perhaps in another table, so as to save space? Although, my primary concern is getting each row added separately.
Thanks