I'm setting up a system using PHP/mySQL where the end user picks a date from a calendar (just a typical Javascript date picker) and also selects "Day" or "Night" using a radio button input, then submits the form with those two values.

Up to five dates and day/night values can be stored per user in total, i.e I have set up these fields in the mySQL users table: Date1, DayNight1, Date2, DayNight2, Date3, DayNight3, Date4, DayNight4, Date5, DayNight5.

When the form is submitted, I want it to populate Date1 and DayNight1, but ONLY if there's nothing already in those fields. If there is, then it should check Date2 and DayNight2, and so on until all fields have been checked. If all fields are full, it will echo a message to say so, and direct the user to a separate page / form where they can delete values as appropriate.

If Date1 and DayNight1 are empty, it should just run the INSERT statement as normal. If they contain values, but Date2 and DayNight2 don't, then run the INSERT statement but with values being entered into the fields for Date2 and DayNight2 instead. So I guess there need to be 4 different INSERT statements in the code depending on the condition of the Date / Daynight fields.

Getting a bit confused with how to put all this into the code though. I've got as far as this but I know it won't work as is (I need to find a way of adding in the other INSERT statements but can't see how)

PHP Code:
<?php
include ('inc/dbconnect.php');
$user =@$_POST['_user'];
$dateavailable =@$_POST['_dateavailable'];
$dayornight =@$_POST['_dayornight'];

if(
$_POST['addavailabledates'])
{
    
if (!empty(
$row['DateAvailable1']))
{
    if (!empty(
$row['DateAvailable2']))
    {
        if (!empty(
$row['DateAvailable3']))
        {
            if (!empty(
$row['DateAvailable4']))
            {
                if (!empty(
$row['DateAvailable5']))
                {
                    echo 
'<p>All your date slots are currently full. <a href="deleteavailabledates.php">Click here</a> to remove some of your date slots.</p>';
                }
                else {
                    
// Build SQL Query  
                    
$query "INSERT INTO users (DateAvailable5, DayOrNight5) VALUES ('$dateavailable', '$dayornight') WHERE Username LIKE '$user'"// specify the table and field names for the SQL query
                    
if($result mysql_query($query)) 
                    { 
                    
header('location: addconfirmed.php');
                    exit;
                    } 
                    else 
                    { 
                    echo 
"ERROR: ".mysql_error(); 
                    }                
                }

            }
        }
    }
}



}
?>
I'm also aware that I need to run an initial query somehow to output the DateAvailable fields for that specific user, to check them- something along the lines of a "SELECT DateAvailable1 (etc) FROM users WHERE Username LIKE '$user1'" though I'm not sure what exact format that should take.

Any ideas?... as this seems quite complex at the moment but probably doesn't need to be.

By the way, the calendar / date picker doesn't allow multiple dates to be added at the same time (and even if it did, obviously this could make it messy when inputting values from the input in the form, into the db table date fields)