Problem on inserting data from foreach to database

Hi,

I have forms that where I type on the process_name the another table will displayed with header Compound, Output and the list of Reject Type which is I displayed using foreach.

Now I I have op_reject table with the fields of:

compound_type
reject_type
reject

now when I add
here are the sample table:

Compound|Output|AA|BB|CC|DD
P1--------10------1–2–3-----
P2--------------------5--------
P3--------50------1---------4

For better understanding

On P1 Compound I have output 10, Reject AA= 1, BB=2, CC=3
On P2 Compound I have no output, Reject BB=5
On P3 Compound I have output 50, Reject AA= 1, DD=4

Now I want to save it like this"

Compound-Reject_type-Reject
P1--------AA----------1
P1--------BB----------2
P1--------CC----------3
P2--------BB----------5
P3--------AA----------1
P3--------DD----------4

here is my index.php


<?php
  error_reporting(0);
 session_start();
  ob_start();
  date_default_timezone_set("Asia/Singapore");
  
  include('connection.php');
  
  $save = isset($_POST['save']);
  
if ($save)
{
    $process_id     = $_POST['process_id']; 
    $section_id     = $_POST['section_id'];
    $section_name   = $_POST['section_name'];  
    $process_name   = $_POST['process_name'];  
    
        if(!$_POST["section_name"])
        {
        $sysmessage = "Section Name is required";
        }
        elseif(!$_POST["process_name"])
        {
          $sysmessage = "Process Name is required";  
        }
        else
        {
        $sql_insert = 
        "INSERT INTO op_reports 
        (op_number, op_date, process_id, supervisor_id, report_date, report_shift, operator_1, operator_2, operator_3, operator_4, operator_5, operator_6, operator_7, operator_8, machine_1, machine_2, machine_3, uom, section_id)
        VALUES
        ('$op_number', '$op_date', '$process_id', '$spv_id', '$process_date', '$shift_id', '$op_1', '$op_2', '$op_3', '$op_4', '$op_5', '$op_6', '$op_7',
        '$op_8', '$ma_1', '$ma_2', '$ma_3', '$uom', '$section_id')";
        
        $res = mysql_query($sql_insert, $con);
        $compound_type     = $_POST['compound_type'];
        $op_output         = $_POST['output'];    
        $reject_type       = $_POST['reject_type'];
        $reject            = $_POST['reject'];    
            
          
        for($s = 0; $s < count($compound_type) ; $s++) 
        {  
            if ($op_output[$s] !="")
            {
                for($i = 0; $i < count($reject_type[$s]) ; $i++)
                { 
                    if($reject[$i] != "")
                    {
                       $sql_re = "INSERT INTO op_reject
                                (compound_type, reject_type, reject)
                                VALUES
                                ('$compound_type[$s]', '$reject_type[$i]', '$reject[$i]')";
                                $result_re = mysql_query($sql_re, $con); 
                
                 }
                        
              }     
            }
         
         } 
        
      echo header("Location:index.php"); 
        }
        
}

   


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<title>Operator's Shift Report </title>
<head>
<link rel="stylesheet" type="text/css" href="op_report.css" />
<link rel="stylesheet" type="text/css" href="calendar.css" />

<script type="text/javascript" src="jquery.js"></script>
<script type='text/javascript' src='jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />

<script type="text/javascript">

//------AJAX-----//
function AJAX(){
        var xmlHttp;
        try{
            xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
            return xmlHttp;
            }
        catch (e){
            try{
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
                return xmlHttp;
                }
            catch (e){
                try{
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                    return xmlHttp;
                    }
                catch (e){
                    alert("Your browser does not support AJAX!");
                    return false;
                    }
                }
            }
        }

//-----get reject---//
function getmat()
        {
           //  if (window.event.keyCode==13 || window.event.keyCode==10) {
            divid = "op_output_fieldset";
            var url = "get_process_reject_list.php";
            var str = "id=" + document.getElementById("process_name").value;
            var xmlHttp = AJAX();
            xmlHttp.onreadystatechange =  function(){
            if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4){
               // document.getElementById(divid).innerHTML=loadingmessage;
                }
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    var jsonart = xmlHttp.responseText;
                    document.getElementById(divid).innerHTML = jsonart;
                    }
                }
            }
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttp.setRequestHeader("Content-length", str.length);
            xmlHttp.setRequestHeader("Connection", "close");
            xmlHttp.send(str);
          
       // }
        }
        
</script>
</head>
<body onload=document.getElementById("section_name").focus();>
<form name="operator_report" action="" method="post" autocomplete="off">
    <!--Tab List -->
<div id="ddcolortabs">
<ul>
<li id="current"> <a href="index.php" title="Operator's Shift Report"><span>Operator's Shift Report</span></a></li>
</ul>
</div>
<script type="text/javascript" src="calendar.js"> </script> 
<div id="operators_report">
<fieldset>
<legend><h1>Operator's Shift Report</h1></legend>
<?php
if($sysmessage)
   {
    echo "<p style='text-align: center; color: #ff0000; font-weight: bold;'>$sysmessage</p>";
   }
 ?>
<table>
<tr>
<td>Section:</td>
<td><input type="text" name="section_name" id="section_name" value="<?php echo $section_name; ?>" size="30"></td>
<td>Process:</td>
<td><input type="text" name="process_name" id="process_name" value="<?php echo $process_name; ?>" size="30" onkeyup="getmat();"></td>
</tr>

</table>

</fieldset>
</div>
<input type="hidden" name="section_id" id="section_id" value="" />
<input type="hidden" name="process_id" id="process_id" value="" />
<div id="op_output_fieldset">
</div>

<div id='btn'>
<input type="submit" name="save" id="save" value="Save">
</div>

</form>
</body>
</html>

and here is the get_process_reject_list


<?php
ob_start();
include "connection.php";
if($_POST["id"])
{  
            echo "<fieldset>";
            echo "<legend><H1> Operator's Output and Reject</H1></legend>";
            echo "<table>";
            echo "<tr>";
            echo "<th>Compound</th>";
            echo "<th>Output</th>";
  
$sql = "select r.reject_acro, r.reject_id from process_list AS p LEFT JOIN reject_list AS r ON 
p.reject_id = r.reject_process_id
 where p.process_name LIKE '" . ($_POST["id"]) . "%'";

 $rsd = mysql_query($sql);
        while($rs = mysql_fetch_assoc($rsd)) {
	        $reject[] = $rs['reject_acro'];
            $reject_id[] = $rs['reject_id'];
        
        }   

            if(empty($reject))
            {
                echo"";
            }
            else
            {   
                    foreach ($reject as $reject)
                    {   
                        echo "<th style='border:none;'><input type='text' name='reject_type[]' id='reject_type' value='$reject' style='border:none;background-color: transparent;text-align:center;color: #FFF; font-weight: bold; ' size='5'></th>";
                    }
             
            echo "</tr>";  
            } 
            
            $sql_comp = "SELECT compound_id, compound_type FROM compound_list ORDER BY compound_type ASC";
            $res = mysql_query($sql_comp);
            echo "<tr>";
            while($comp = mysql_fetch_assoc($res)){
                $compound_type = $comp['compound_type'];
                echo "<td style='border:none;'><input type='text' name='compound_type[]' id='compound_type' value='$compound_type' style='border:none;' size='10'></td>";
                echo "<td style='border:none;'><input type='text' name='output[]' id='output' value='' size='7'></td>";
                
                foreach($reject_id AS $reject)
                {
                echo "<td style='border:none;'><input type='text' name='reject[]' id='reject' value='' size='7'></td>";   
                }
                
                echo "</tr>";
          }  
            echo "</table>";
            echo "</fieldset>";
        

 }
?>

the data saved on my table is:

Compound|Output|AA|BB|CC|DD
P1--------10------1–2–3-----
P2--------------------5--------
P3--------50------1---------4

For better understanding

On P1 Compound I have output 10, Reject AA= 1, BB=2, CC=3
On P2 Compound I have no output, Reject BB=5
On P3 Compound I have output 50, Reject AA= 1, DD=4

Now I want to save it like this"

Compound-Reject_type-Reject
P1--------AA----------1
P2--------AA----------1

I hope somebody can help me on this.

Its new to me to encountered this kind of flow.

Thank you so much.