How to compare two multiple values from database and post in PHP

I want to compare post values like php, java with the value stored in database table name courses and field name subjects and it has values php, java, c++. After comparing all values it must give results like courses already taken.

I want to store values then if values available then compare values and if new value the store these values along with old values.

1 . Store values --> Done
2. Compare values --> Is my problem
3. Store new values with old values --> Done

<pre><?php 

include './connect.php';


echo "<form action='admin.php?page=selectcourse' method='POST'>";

echo "<h1>Assign Course to Students</h1>";

$res=mysqli_query($conn, "SELECT * FROM inputcourses");

$counter = 1;

$product_count = mysqli_num_rows ($res); 




if ($product_count > 0) {


	while($row=mysqli_fetch_array($res))
	{?>


		<br/>
		<input type="checkbox" name="coursename[]" value=" <?php echo $row['coursename']; ?>"> <?php echo $row['coursename']; ?>
<?php
		 
		$counter++;
	}
	
}

else
{
	echo "No Course Found";
}


echo "<h1>Select Student</h1>";

$res=mysqli_query($conn, "SELECT * FROM registration");


$counter = 1;

$student_count = mysqli_num_rows ($res); 




if ($student_count > 0) {
	echo "<table border=1 width='800px'>";
	echo "<tr>
		<td></td>
		<td>Sr no.</td>
		<td>Name</td>
		<td>Student ID</td>
		</tr>";
	while($row=mysqli_fetch_array($res))
	{

		echo "<tr>
		<td><input type='radio' name='rname' value='".$row['id']."'></td>
		<td>".$counter."</td>
		<td><input type='text' name='name' value='".$row['first_name'].' '.$row['last_name']."' readonly></td>
		<td><input type='text' name='id' value='".$row['id']."' readonly></td>
		</tr>";	
		 
		$counter++;
	}
		echo "</table>";
	
}

else
{
	echo "No Student Found";
}

echo "<input type='submit' name='submit' value='Submit'>";

echo "</form>";


    ?>
<pre><?php
include 'connect.php';
if (isset($_POST['submit'])) {
$id = mysqli_real_escape_string($conn, $_POST['rname']);
$checkBox = strtoupper(implode(',', $_POST['coursename']));

$res=mysqli_query($conn, "SELECT * FROM  registration WHERE id='$id'");
$row=mysqli_fetch_array($res);

$newval = $row['subjects'].','.$checkBox;



if (empty($row['subjects'])) {
   # code...
   $result1=mysqli_query($conn, "UPDATE registration set subjects='" . $checkBox. "' WHERE id = '".$id."'");
   if ($result1) {
      # code...
      header("Location: admin.php?page=assigncourse&&message=done from with empty");
   }
   else
   {
      header("Location: admin.php?page=assigncourse&&message=not done");

   }
}

elseif (!empty($row['subjects'])) {
   # code...
   $res=mysqli_query($conn, "SELECT * FROM  registration WHERE FIND_IN_SET('php', subjects)");
   if ($res) {
      # code...
      echo "taken";
   }
   
   else{
      $result1=mysqli_query($conn, "UPDATE registration set subjects='" . $newval. "' WHERE id = '".$id."'");
      if ($result1) {
         # code...
         header("Location: admin.php?page=assigncourse&&message=done from else");
      }
      else
      {
         header("Location: admin.php?page=assigncourse&&message=not done");

      }
   }







}


}

?>