Currently, when you hit submit, you build the HTML output with the old data, then update the database. You need to first update the database and then get the data to build the UI. Your updated code should look like this:
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<?php
//Connect to the database
require_once("dboconn.php");
$conn=ConnectionFactory::connect();
if (isset($_POST['programmers'])){
require_once("dboconn.php");
$conn=ConnectionFactory::connect();
$query="DELETE FROM programmer WHERE programmer_id=:programmer_id";
$query2="DELETE FROM project WHERE programmer_id=:programmer_id";
$query3="SELECT programmer_name, programmer_surname FROM programmer WHERE programmer_id=:programmer_id";
$pr_stmt=$conn->prepare($query);
$pr_stmt2=$conn->prepare($query2);
$pr_stmt3=$conn->prepare($query3);
$affected_rows=0;
$notWorking=false; // set this variable to check if a programmer is working or not on any project
$surnameDeletedProgs=array();
$nameDeletedProgs=array();
foreach($_POST['programmers'] as $programmer_id){
$pr_stmt->bindValue(':programmer_id',$programmer_id);
$pr_stmt2->bindValue(':programmer_id',$programmer_id);
$pr_stmt3->bindValue(':programmer_id',$programmer_id);
$pr_stmt3->execute();
//Get the names and surnames of the programmers who were deleted from the database and store them in arrays
$result=$pr_stmt3->fetch();
array_push($nameDeletedProgs,$result['programmer_name']);
array_push($surnameDeletedProgs,$result['programmer_surname']);
//Delete the programmer from the database
$affected_rows+=$pr_stmt->execute();
//If they were working on a project, delete the project also from the 'project' table
if(projects($programmer_id)!="Working on no projects at the moment"){
$pr_stmt2->execute();
echo "Also deleted the project they were working on";
}
else $notWorking=true;
}
//If they are not working on any project display this particular message
if ($notWorking){
echo "Hopefully, they were not working on any project at the moment so we just ";
}
//if there were no checkboxes selectes, display a message to tell people to select at least one programmer
if ($affected_rows==0){
echo "No programmers to delete. Please select at least one.";
exit;
}
//display how many programmers were deleted from the 'programmers' table and also what are their names
else{
echo "deleted ".$affected_rows." programmer(s)";
echo ". Successfully deleted:<ul>";
for($i=0;$i<count($nameDeletedProgs);$i++){
echo "<li>".$nameDeletedProgs[$i]." ".$surnameDeletedProgs[$i]."</li>";
}
echo "</ul>";
}
}
$query = "SELECT * FROM programmer";
$resultset = $conn->query($query);
while ($row = $resultset->fetch())
{
echo "<p class='col'>";
echo "<input type='checkbox' name='programmers[]' id='programmer".$row['programmer_id']."' value='".$row['programmer_id']."'>";
echo "<a href='' class='help tip-below' data-tip='".projects($row['programmer_id'])."'><label for='programmer".$row['programmer_id']."'> ".$row['programmer_name']." ".$row['programmer_surname']."</label></a>";
echo "</p>";
}
?>
<input type="submit" value="Delete Programmer(s)" class="sub-del submit-btn">
</form>