I have a feeling there's a ridiculously simple solution to the issue I'm having, but I don't know enough about the intricacies of array functions to figure it out It has to do with sorting through $_POST values such that those that edit an existing record get sent to an UPDATE query, while those that generate a new one go to INSERT. See the attached screenshot of my form-in-progress - basically a matrix of stations to sign up for by open time blocks (the numbers in the table cells are IDs for slot, station, etc).
Here's the code:
<?php
$course = $_POST['course'];
$week = $_POST['week'];
$dayID = $_POST['day'];
$time = $_POST['time'];
$time = array_diff($time, array('null')); //remove null values
$station = $_POST['station'];
$station = array_diff($station, array('blank')); //remove null values
$slot = $_POST['slot'];
$slot = array_diff($slot, array('null')); //remove null values
if (isset($slot)) {
foreach ($slot as $slotID) {
foreach ($station as $stnExpID) {
$stnExpID = explode('-',$stnExpID); //split the stationID from the expID
$stationID = $stnExpID[0];
$expID = $stnExpID[1];
foreach ($time as $timeID) {
echo "UPDATE slotTest SET stationID=$stationID, expID=$expID, timeID=$timeID, open=1 WHERE slotID=$slotID;";
echo "<br/>";
}
}
}
} else {
echo "<br />";
$query = "INSERT INTO slotTest(courseID,weekID,dayID,timeID,stationID,expID,open) VALUES <br />(";
foreach ($station as $stnExpID) {
$stnExpID = explode('-',$stnExpID); //split the stationID from the expID
$stationID = $stnExpID[0];
$expID = $stnExpID[1];
foreach ($time as $timeID) {
$query .= "$course,$week,$dayID,$timeID,$stationID,$expID,1)";
$query .= ",<br/>(";
}
}
$query = substr($query, 0, -2); // strips the last ,( characters
$query .= ";";
echo $query;
//echo makeSlot($query);
}
?>
...and here's an example of what it generates:
UPDATE slotTest SET stationID=1, expID=1, timeID=1, open=1 WHERE slotID=43;
UPDATE slotTest SET stationID=1, expID=1, timeID=2, open=1 WHERE slotID=43;
UPDATE slotTest SET stationID=2, expID=2, timeID=1, open=1 WHERE slotID=43;
UPDATE slotTest SET stationID=2, expID=2, timeID=2, open=1 WHERE slotID=43;
UPDATE slotTest SET stationID=3, expID=4, timeID=1, open=1 WHERE slotID=43;
UPDATE slotTest SET stationID=3, expID=4, timeID=2, open=1 WHERE slotID=43;
UPDATE slotTest SET stationID=1, expID=1, timeID=1, open=1 WHERE slotID=44;
UPDATE slotTest SET stationID=1, expID=1, timeID=2, open=1 WHERE slotID=44;
UPDATE slotTest SET stationID=2, expID=2, timeID=1, open=1 WHERE slotID=44;
UPDATE slotTest SET stationID=2, expID=2, timeID=2, open=1 WHERE slotID=44;
UPDATE slotTest SET stationID=3, expID=4, timeID=1, open=1 WHERE slotID=44;
etc.
The problem is pretty glaring - the INSERT query doesn't work at all, and the UPDATE repeats itself where it shouldn't. I can see why the latter is happening - because of the nested foreach loops - but I can't for the life of me figure out what method to use instead. All I want it to do is:
1) Check if slot exists - if yes, write the associated values for station, experiment, and time...if not...
2) Take whatever station, experiment, and time values are left over and create new slots.
There's got to be a better way to do this, and I'd be most grateful for any advice.