the following is a 'tricky' example of INSERTing data that satisfies a maximum count of rows -
$query = "INSERT INTO team_members (team_id, staff_id, stafftype)
SELECT -- the following values being SELECTed are the actual data values to insert
?,?,? FROM DUAL -- dual is an allowed dummy table name to satisfy the FROM ... WHERE syntax
WHERE (SELECT COUNT(*) FROM team_members WHERE team_id = ? AND stafftype='leader') < 1 -- insert the data if the WHERE (subquery count) < 1 is TRUE";
$stmt = $pdo->prepare($query);
$stmt->execute([$team_id, $staff_id, $stafftype, $team_id]);
if($stmt->rowcount())
{
echo "A leader row was inserted for team_id: $team_id, staff_id: $staff_id<br>";
} else {
echo "A leader already exists for team_id: $team_id<br>";
}
this example was to insert a maximum of one 'leader' row per team id. you would change it to insert a maximum of two rows per datetime appointment slot.
because this uses a single query to both get a count of the number of existing rows and insert a new row, it will work correctly for multiple concurrent instances of your script.