Need help in saving data from textboxes inside foreach loop

Hi,

It’s my first time to used foreach loop to display data from database and also used foreach loop to display textboxes

Now, I felt difficulties in saving data from the textboxes displayed by foreach.

Here is my code:


<html>
<head>
</head>
<body>
<form name="operator_report" action="" method="post" autocomplete="off">
<?php
ob_start();
include "connection.php";

$save = isset($_POST['save']);

//insert data to database
if($save)
{
    $compound_type     = $_POST['compound_type'];
    $op_output         = $_POST['output'];
    $c_output         = $_POST['output'];
    $reject_type[]       = $_POST['reject_type'];
    $reject[]            = $_POST['reject'];


    for($s = 0; $s < count($compound_type) ; $s++)
    {
            //---save operators output to op_output, in this part i don't have problem in saving the data saved correctly
            if ($op_output[$s] !="")
            {
                $sql = "INSERT INTO op_output
                (op_number, process_id, shift_id, shift_date, shift_type, compound_type, compound_output)
                VALUES
                ('001', '4', '1','2013-07-16', '1', '$compound_type[$s]', '$op_output[$s]')";
               $result = mysql_query($sql, $con);
            }


            //---this part I need help in saving reject.
            //---I guess i have the problem here in looping ...
            //---this code did not work..
            for($i = 0; $i < count($reject_type[$s]) ; $i++)
            {

             if($reject[$i] != "")
                  {
                       $sql_re = "INSERT INTO op_reject
                                (op_number, compound_type, reject_type, reject)
                                VALUES
                                ('001','$compound_type[$s]', '$reject_type[$i]', '$reject[$i]')";
                                $result_re = mysql_query($sql_re, $con);
                  }
            }
   }
}

$id = 'Final Mix';
if($id == 'Final Mix')
{
            echo "<fieldset>";
            echo "<legend><H1> Operator's Output and Reject</H1></legend>";

            echo "<table>";
            echo "<tr>";
            echo "<th>Compound</th>";
            echo "<th>Output</th>";
//query to select reject acronym
$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 '" .$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
            {
                    //display reject acronym as header
                    foreach ($reject as $reject)
                    {
                      echo "<th style='border:none;'><input type='text' name='reject_type[]' id='reject_type' value='$reject' style='border:none; font-weight: bold; ' size='5'></th>";
                    }

            echo "</tr>";
            }

            //query select compound_type
            $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'];

                //---display compound_type
                echo "<td style='border:none;'><input type='text' name='compound_type[]' id='compound_type' value='$compound_type' style='border:none;' size='10'readonly='readonly'></td>";
                //---input box for output
                echo "<td style='border:none;'><input type='text' name='output[]' id='output' value='' size='7'></td>";

                //----i used foreach to loop textbox in every reject acronym.
                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>";

 }
?>
<input type="submit" name="save" id="save" value="Save">
</form>
</body>
</html>

I also attached my sample output screenshots and the database.

Here is the sample scenario.
Compound Type—Output—SC—SP—SH—
P28--------------10-------------1-----2
P32--------------20--------5---------------
P32NW--------------------------3----------

I need to save data on op_reject like this:

op_number—compound_type----reject_type—reject
001----------P28---------------SP------------1
001----------P28---------------SH------------2
001----------P32---------------SC------------5
001----------P32NW------------SP------------3

Sad to say in my code in saving using forloop it did not work.

I post it because i am hoping somebody can help me, but I also tried my best to fixed this.

Any help is highly appreciated.

Thank you so much.

Hi,
Maybe the problem is that it is added the same name to this line:

foreach ($reject as $reject)

You must add a diferent name for value, after “as”.

foreach ($reject as $reject_v)

In the rest, to much code to check.

I don’t have problem in displaying rejects. But you mean it will affect in saving?

Thank you.

Hi,

I tried to change it as you said.

and now here is my code:


<html>
<head>
</head>
<body>
<form name="operator_report" action="" method="post" autocomplete="off">
<?php
ob_start();
include "connection.php";

$save = isset($_POST['save']);

//insert data to database
if($save)
{
    $compound_type     = $_POST['compound_type'];
    $op_output         = $_POST['output'];
    $c_output         = $_POST['output'];
    $reject_type       = $_POST['reject_type'];
    $reject            = $_POST['reject'];


    for($s = 0; $s < count($compound_type) ; $s++)
    {
            //---save operators output to op_output, in this part i don't have problem in saving the data saved correctly
            if ($op_output[$s] !="")
            {
                $sql = "INSERT INTO op_output
                (op_number, process_id, shift_id, shift_date, shift_type, compound_type, compound_output)
                VALUES
                ('001', '4', '1','2013-07-16', '1', '$compound_type[$s]', '$op_output[$s]')";
               $result = mysql_query($sql, $con);
            }


            //---this part I need help in saving reject.
            //---I guess i have the problem here in looping ...
            //---this code did not work..

            if($op_output[$s] != "")
            {
            for($i = 0; $i < count($reject_type[$s]) ; $i++)
            {
           in this part i got problem
           if($reject[$i] != "")
                  {
                       $sql_re = "INSERT INTO op_reject
                                (op_number, compound_type, reject_type, reject)
                                VALUES
                                ('001','$compound_type[$i]', '$reject_type[$i]', '$reject[$i]')";
                                $result_re = mysql_query($sql_re, $con);

           }
            }
         }
   }
}

$id = 'Final Mix';
if($id == 'Final Mix')
{
            echo "<fieldset>";
            echo "<legend><H1> Operator's Output and Reject</H1></legend>";

            echo "<table>";
            echo "<tr>";
            echo "<th>Compound</th>";
            echo "<th>Output</th>";
//query to select reject acronym
$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 '" .$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
            {
                    //display reject acronym as header
                    foreach ($reject as $reject_v)
                    {
                      echo "<th style='border:none;'><input type='text' name='reject_type[]' id='reject_type' value='$reject_v' style='border:none; font-weight: bold; ' size='5'></th>";
                    }

            echo "</tr>";
            }

            //query select compound_type
            $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'];

                //---display compound_type
                echo "<td style='border:none;'><input type='text' name='compound_type[]' id='compound_type' value='$compound_type' style='border:none;' size='10'readonly='readonly'></td>";
                //---input box for output
                echo "<td style='border:none;'><input type='text' name='output[]' id='output' value='' size='7'></td>";

                //----i used foreach to loop textbox in every reject acronym.
                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>";

 }
?>
<input type="submit" name="save" id="save" value="Save">
</form>
</body>
</html>

but the problem is as you can see my first reject type i have no value, so it did not save all.

I think my problem in on looping

Are you sure op_number is NOT a primary index? Other than that you should escape POST values especially from textareas.

    $compound_type     = mysql_real_escape_string(trim($_POST['compound_type']));
    $op_output         = mysql_real_escape_string(trim($_POST['output']));
    $c_output         = mysql_real_escape_string(trim($_POST['output']));
    $reject_type       = mysql_real_escape_string(trim($_POST['reject_type']));
    $reject            = mysql_real_escape_string(trim($_POST['reject']));  

IN MHO I also would check for !empty() instead of comparing to !=“”

if(!empty($op_output[$s]))

When using array[key] pairs in a query they should be surrounded with curly brackets.

VALUES
                                ('001','{$compound_type[$i]}', '{$reject_type[$i]}', '{$reject[$i]}')

Anyway, these are few things I spotted.

here is my new code:


<html>
<head>
</head>
<body>
<form name="operator_report" action="" method="post" autocomplete="off">
<?php
ob_start();
include "connection.php";

$save = isset($_POST['save']);

//insert data to database
if($save)
{
    $compound_type     = $_POST['compound_type'];
    $op_output         = $_POST['output'];   
    $c_output         = $_POST['output'];  
    $reject_type       = $_POST['reject_type'];
    $reject            = $_POST['reject'];  
    
    
    for($s = 0; $s < count($compound_type) ; $s++) 
    {  
            //---save operators output to op_output, in this part i don't have problem in saving the data saved correctly
            if(!empty($op_output[$s]))  
            {
                $sql = "INSERT INTO op_output 
                (op_number, process_id, shift_id, shift_date, shift_type, compound_type, compound_output)
                VALUES
                ('001', '4', '1','2013-07-16', '1', '$compound_type[$s]', '$op_output[$s]')";
               $result = mysql_query($sql, $con);
            }
   }
   //var_dump($reject_type);
   
   //----code to save reject data---//
 for($i = 0; $i < count($reject_type) ; $i++)

    { 

    for($x = 0; $x < count($compound_type[$i]) ; $x++) 

    {       

          if(!empty($reject[$i]))  


          {

                      $sql_re = "INSERT INTO op_reject

                                (op_number, compound_type, reject_type, reject)

                                VALUES

                                ('001','{$compound_type[$x]}', '{$reject_type[$i]}', '{$reject[$i]}')";
                                
                           
                                $result_re = mysql_query($sql_re, $con);

                   

          }

          else

          {

             

          }

    }

    }


}

$id = 'Final Mix';
if($id == 'Final Mix')
{  
            echo "<fieldset>";
            echo "<legend><H1> Operator's Output and Reject</H1></legend>";

            echo "<table>";
            echo "<tr>";
            echo "<th>Compound</th>";
            echo "<th>Output</th>";
//query to select reject acronym  
$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 '" .$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
            {   
                    //display reject acronym as header               
                    foreach ($reject as $reject_v)
                    {   
                      echo "<th style='border:none;'><input type='text' name='reject_type[]' id='reject_type' value='$reject_v' style='border:none; font-weight: bold; ' size='5'></th>";
                    }
             
            echo "</tr>";  
            } 
            
            //query select compound_type
            $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'];
                
                //---display compound_type
                echo "<td style='border:none;'><input type='text' name='compound_type[]' id='compound_type' value='$compound_type' style='border:none;' size='10'readonly='readonly'></td>";
                //---input box for output
                echo "<td style='border:none;'><input type='text' name='output[]' id='output' value='' size='7'></td>";
                
                //----i used foreach to loop textbox in every reject acronym.
                foreach($reject_id AS $reject_i)
                {
                echo "<td style='border:none;'><input type='text' name='reject[]' id='reject' value='' size='7'></td>";   
                }
                
                echo "</tr>";
          }  
            echo "</table>";
            echo "</fieldset>";

 }
?>
<input type="submit" name="save" id="save" value="Save">
</form>
</body>
</html>

the op_number is not primary key.

The output of this:

001 P28 SP 1.00
001 P28 SH 2.00

it should be like this :

op_number—compound_type----reject_type—reject
001----------P28---------------SP------------1
001----------P28---------------SH------------2
001----------P32---------------SC------------5
001----------P32NW------------SP------------3

:frowning:

Thank you…

I figured out that my problem is in looping, but I don’t know how can I resolved it :frowning:

Thank you for your help .

Without being able to test anything, here’s MY take on your project.

<?php
ob_start();
include "connection.php";

//Define used common values
$op_number = "001";
$process_id = "4";
$shift_id = "1";
$shift_date = "2013-07-16";
$shift_type = "1";
							
	if (isset($_POST['save'])){

		foreach($_POST['compound_id'] as $k => $compound_id){
			//Use array keys to identify items in loop
			$compound_type = $_POST['compound_type'][$k];
			$op_output = $_POST['output'][$k];
			
			if(!empty($op_output)){
				//Run our loop to build value array for insert			
				$values[] = "('$op_number', '$process_id', '$shift_id','$shift_date', '$shift_type', '$compound_type', '$op_output')";
			}
				
			/////Start sideways loop////
			
			//Here we double check that the array $_POST['reject'] has the primary $compound_id key
			if(isset($_POST['reject']) && array_key_exists($compound_id,$_POST['reject'])){
			
				//If found we run through this sub array to get the reject and reject_type by subkey
				foreach($_POST['reject'][$compound_id] as $subkey => $reject){
				
					//check if not empty
					if(!empty($reject)){
					
						//Again we use corrosponding key to get reject_type
						$reject_type = $_POST['reject_type'][$subkey];
						
						//Run our loop to build value array for insert
						$rejectvalues[] = "('$op_number','$compound_type', '$reject_type', '$reject')";						
					
					}				
				}			
			}				
		}
		
		//insert data to database
		if(isset($values)){
			$sql = "INSERT INTO op_output
					(op_number, process_id, shift_id, shift_date, shift_type, compound_type, compound_output)
	                VALUES ";
			$sql .= implode(",",$values);
			//echo "$sql<br />";
			$result = mysql_query($sql, $con);
		}
		
		
		//insert data to database
		if(isset($rejectvalues)){
			$sql_re = "INSERT INTO op_reject
					(op_number, compound_type, reject_type, reject)
					 VALUES ";				
			$sql_re .= implode(",",$rejectvalues);
			//echo "$sql_re<br />";
			$result = mysql_query($sql_re, $con);	
		}
		
	}//if (isset($_POST['save'])){
											
//////////////////////////////////////////////
//////////////////////////////////////////////
$id = 'Final Mix';
if($id == 'Final Mix'){
//////////////////////////////////////////////
//////////////////////////////////////////////

	//Build display data BEFORE output
	$display = "";
	
	//query to select reject acronym
	$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 '" .$id. "%'";
	$rsd = mysql_query($sql);
	while($rs = mysql_fetch_assoc($rsd)) {
		$reject[] = $rs['reject_acro'];
		$reject_id[] = $rs['reject_id'];
	}
	
	if(!isset($reject) || isset($reject) && empty($reject)){
		$display .= "";
	}else{
		//display reject acronym as header
		foreach ($reject as $reject_v){
			$display .= "<th style='border:none;'><input type='text' name='reject_type[]' value='$reject_v' style='border:none; font-weight: bold; ' size='5' /></th>";
		}
		
		$display .= "</tr>";
	}
		
	//query select compound_type
	$sql_comp = "SELECT compound_id, compound_type FROM compound_list ORDER BY compound_type ASC";
	$res = mysql_query($sql_comp);

	if(mysql_num_results($res)){
		
		while($comp = mysql_fetch_assoc($res)){	
		
		$display .= "<tr>";

			//---display compound_type
			$display .= "<td style='border:none;'>
			<input type='text' name='compound_type[]' value='$compound_type' style='border:none;' size='10' />
			<input type='hidden' name='compound_id[]' value='$compound_id' />
			</td>";
			//---input box for output
			$display .= "<td style='border:none;'><input type='text' name='output[]' value='' size='7' /></td>";
			
			//----i used foreach to loop textbox in every reject acronym.
			foreach($reject_id AS $reject_i)				{
				$display .= "<td style='border:none;'><input type='text' name='reject[$compound_id][]' value='' size='7' /></td>";
			}
			
			$display .= "</tr>";
		}
	}
}//if($id == 'Final Mix'){      		

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<title>Operator's Output and Reject</title>
	</head>
	<body>
		<form name="operator_report" action="" method="post">
			<fieldset>
				<legend style="font-size:36px;"> Operator's Output and Reject</legend>
				<table>
					<tr>
						<th>Compound</th>
						<th>Output</th>
						<?php
						if(isset($display)){
							echo "$display";
						}
						?>
				</table>
			</fieldset>
			<input type="submit" name="save" id="save" value="Save" />
		</form>
	</body>
</html>

I tried to used your suggested code but no display on my webpage… ot was only blank.

Thank you

It works now, I tried to edit something.


<?php
ob_start();
include "connection.php";

//Define used common values
$op_number = "001";
$process_id = "4";
$shift_id = "1";
$shift_date = "2013-07-16";
$shift_type = "1";

    if (isset($_POST['save'])){

        foreach($_POST['compound_id'] as $k => $compound_id){
            //Use array keys to identify items in loop
            $compound_type = $_POST['compound_type'][$k];
            $op_output = $_POST['output'][$k];

            if(!empty($op_output)){
                //Run our loop to build value array for insert
                $values[] = "('$op_number', '$process_id', '$shift_id','$shift_date', '$shift_type', '$compound_type', '$op_output')";
            }

            /////Start sideways loop////

            //Here we double check that the array $_POST['reject'] has the primary $compound_id key
            if(isset($_POST['reject']) && array_key_exists($compound_id,$_POST['reject'])){

                //If found we run through this sub array to get the reject and reject_type by subkey
                foreach($_POST['reject'][$compound_id] as $subkey => $reject){

                    //check if not empty
                    if(!empty($reject)){

                        //Again we use corrosponding key to get reject_type
                        $reject_type = $_POST['reject_type'][$subkey];

                        //Run our loop to build value array for insert
                        $rejectvalues[] = "('$op_number','$compound_type', '$reject_type', '$reject')";

                    }
                }
            }
        }

        //insert data to database
        if(isset($values)){
            $sql = "INSERT INTO op_output
                    (op_number, process_id, shift_id, shift_date, shift_type, compound_type, compound_output)
                    VALUES ";
            $sql .= implode(",",$values);
            //echo "$sql<br />";
            $result = mysql_query($sql, $con);
        }


        //insert data to database
        if(isset($rejectvalues)){
            $sql_re = "INSERT INTO op_reject
                    (op_number, compound_type, reject_type, reject)
                     VALUES ";
            $sql_re .= implode(",",$rejectvalues);
            //echo "$sql_re<br />";
            $result = mysql_query($sql_re, $con);
        }

    }//if (isset($_POST['save'])){

//////////////////////////////////////////////
//////////////////////////////////////////////
$id = 'Final Mix';
if($id == 'Final Mix'){
//////////////////////////////////////////////
//////////////////////////////////////////////

    //Build display data BEFORE output
    $display = "";

    //query to select reject acronym
    $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 '" .$id. "%'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_assoc($rsd)) {
        $reject[] = $rs['reject_acro'];
        $reject_id[] = $rs['reject_id'];
    }

    if(!isset($reject) || isset($reject) && empty($reject)){
        $display .= "";
    }else{
        //display reject acronym as header
        foreach ($reject as $reject_v){
            $display .= "<th style='border:none;'><input type='text' name='reject_type[]' value='$reject_v' style='border:none; font-weight: bold; ' size='5' /></th>";
        }

        $display .= "</tr>";
    }

    //query select compound_type
    $sql_comp = "SELECT compound_id, compound_type FROM compound_list ORDER BY compound_type ASC";
    $res = mysql_query($sql_comp);

    if(mysql_num_rows($res)){

        while($comp = mysql_fetch_assoc($res)){

        $compound_type = $comp['compound_type'];
        $compound_id   = $comp['compound_id'];

        $display .= "<tr>";

            //---display compound_type
            $display .= "<td style='border:none;'>
            <input type='text' name='compound_type[]' value='$compound_type' style='border:none;' size='10' />
            <input type='hidden' name='compound_id[]' value='$compound_id' />
            </td>";
            //---input box for output
            $display .= "<td style='border:none;'><input type='text' name='output[]' value='' size='7' /></td>";

            //----i used foreach to loop textbox in every reject acronym.
            foreach($reject_id AS $reject_i)                {
                $display .= "<td style='border:none;'><input type='text' name='reject[$compound_id][]' value='' size='7' /></td>";
            }

            $display .= "</tr>";
        }
    }
}//if($id == 'Final Mix'){

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Operator's Output and Reject</title>
    </head>
    <body>
        <form name="operator_report" action="" method="post">
            <fieldset>
                <legend style="font-size:36px;"> Operator's Output and Reject</legend>
                <table>
                    <tr>
                        <th>Compound</th>
                        <th>Output</th>
                        <?php
                        if(isset($display)){
                            echo "$display";
                        }
                        ?>
                </table>
            </fieldset>
            <input type="submit" name="save" id="save" value="Save" />
        </form>
    </body>
</html>

Thank you

Looks like I took out a few lines when editing the POST version. Any better?
EDIT: I see you noticed the same lines. Very good.

<?php
ob_start();
include "connection.php";

//Define used common values
$op_number = "001";
$process_id = "4";
$shift_id = "1";
$shift_date = "2013-07-16";
$shift_type = "1";
							
	if (isset($_POST['save'])){

		foreach($_POST['compound_id'] as $k => $compound_id){
			//Use array keys to identify items in loop
			$compound_type = $_POST['compound_type'][$k];
			$op_output = $_POST['output'][$k];
			
			if(!empty($op_output)){
				//Run our loop to build value array for insert			
				$values[] = "('$op_number', '$process_id', '$shift_id','$shift_date', '$shift_type', '$compound_type', '$op_output')";
			}
				
			/////Start sideways loop////
			
			//Here we double check that the array $_POST['reject'] has the primary $compound_id key
			if(isset($_POST['reject']) && array_key_exists($compound_id,$_POST['reject'])){
			
				//If found we run through this sub array to get the reject and reject_type by subkey
				foreach($_POST['reject'][$compound_id] as $subkey => $reject){
				
					//check if not empty
					if(!empty($reject)){
					
						//Again we use corrosponding key to get reject_type
						$reject_type = $_POST['reject_type'][$subkey];
						
						//Run our loop to build value array for insert
						$rejectvalues[] = "('$op_number','$compound_type', '$reject_type', '$reject')";						
					
					}				
				}			
			}				
		}
		
		//insert data to database 
		if(isset($values)){
			$sql = "INSERT INTO op_output 
					(op_number, process_id, shift_id, shift_date, shift_type, compound_type, compound_output)
	                VALUES ";
			$sql .= implode(",",$values); 
			//echo "$sql<br />";
			$result = mysql_query($sql, $con);
		} 
		
		
		//insert data to database
		if(isset($rejectvalues)){  
			$sql_re = "INSERT INTO op_reject
					(op_number, compound_type, reject_type, reject)
					 VALUES ";				
			$sql_re .= implode(",",$rejectvalues); 
			//echo "$sql_re<br />";
			$result = mysql_query($sql_re, $con);	
		}
		
	}//if (isset($_POST['save'])){ 
											 
//////////////////////////////////////////////
//////////////////////////////////////////////
$id = 'Final Mix';
if($id == 'Final Mix'){
//////////////////////////////////////////////
////////////////////////////////////////////// 
 
	//Build display data BEFORE output
	$display = "";
	
	//query to select reject acronym  
	$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 '" .$id. "%'";
	$rsd = mysql_query($sql);
	while($rs = mysql_fetch_assoc($rsd)) {
		$reject[] = $rs['reject_acro'];
		$reject_id[] = $rs['reject_id'];
	}
	
	if(!isset($reject) || isset($reject) && empty($reject)){
		$display .= "";
	}else{   
		//display reject acronym as header               
		foreach ($reject as $reject_v){   
			$display .= "<th style='border:none;'><input type='text' name='reject_type[]' value='$reject_v' style='border:none; font-weight: bold; ' size='5' /></th>";
		}
		
		$display .= "</tr>";  
	} 
		
	//query select compound_type
	$sql_comp = "SELECT compound_id, compound_type FROM compound_list ORDER BY compound_type ASC";
	$res = mysql_query($sql_comp, $con);

	if(mysql_num_results($res)){
		
		while($comp = mysql_fetch_assoc($res)){	
		$compound_type = $comp['compound_type'];
		$compound_id = $comp['compound_id'];
		$display .= "<tr>";

			//---display compound_type
			$display .= "<td style='border:none;'>
			<input type='text' name='compound_type[]' value='$compound_type' style='border:none;' size='10' />
			<input type='hidden' name='compound_id[]' value='$compound_id' />
			</td>";
			//---input box for output
			$display .= "<td style='border:none;'><input type='text' name='output[]' value='' size='7' /></td>";
			
			//----i used foreach to loop textbox in every reject acronym.
			foreach($reject_id AS $reject_i)				{
				$display .= "<td style='border:none;'><input type='text' name='reject[$compound_id][]' value='' size='7' /></td>";   
			}
			
			$display .= "</tr>";
		}
	}
}//if($id == 'Final Mix'){      		  

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<title>Operator's Output and Reject</title>
	</head>
	<body>
		<form name="operator_report" action="" method="post">
			<fieldset>
				<legend style="font-size:36px;"> Operator's Output and Reject</legend>
				<table>
					<tr>
						<th>Compound</th>
						<th>Output</th>
						<?php
						if(isset($display)){
							echo "$display";
						}
						?>
				</table>
			</fieldset>
			<input type="submit" name="save" id="save" value="Save" />
		</form>
	</body>
</html>

Thank you so much :slight_smile:

Hi,

My problem in saving data was now solved thank you to you.

Now, I just want to display what I save.

Like before I save data on table op_output:

output_id op_number --process_id shift_date -shift_id shift_type compound_type compound_output
1 -----SR130719003- 4--------- 2013-07-19 -3------ 1--------- P28------------ 10.00
2------SR130719003-4----------2013-07-19 -3 ------1--------- P32 ------------20.00

and op_reject table:
reject_id op_number -compound_type reject_type reject
1 —SR130719003- P28------------ BS-------- 1.00
2— SR130719003- P28 ------------C ---------2.00
3— SR130719003- P28 ------------DH-------- 3.00
4 —SR130719003 -P32 ------------D--------- 4.00
5 —SR130719003 -P32 -----------INC-------- 5.00
6 —SR130719003 -P32NW-------- D---------- 6.00
7 —SR130719003- P32NW-------- INC-------- 7.00

Now I just want it to display like this:

Compound Output BS C DH D INC
P28-------10-----1–2-3--------
P32-------20-------------4–5–
P32NW-------------------6–7–

Thank you

Now, I tried to code it:


<?php
ob_start();
include "connection.php";
$id = 'SR130719003';
if($id='SR130719003')
{

    //Build display data BEFORE output
    $display = "";

    //query to select reject acronym
    $sql = "SELECT op_number, reject_type, reject
FROM op_reject WHERE op_number LIKE '" .$id. "%'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_assoc($rsd)) {
        $reject_type[] = $rs['reject_type'];
        $reject_output[] = $rs['reject'];
    }

    if(!isset($reject_type) || isset($reject_type) && empty($reject_type)){
        $display .= "";
    }else{
        //display reject acronym as header
        foreach ($reject_type as $reject_v){
            $display .= "<th style='border:none;'><input type='text' name='reject_type[]' value='$reject_v' style='border:none;background-color: transparent;text-align:center;color: #FFF; font-weight: bold; ' size='5' /></th>";
        }

        $display .= "</tr>";
    }

    //query select compound_type
    $sql_comp = "SELECT DISTINCTcompound_type FROM op_reject WHERE op_number LIKE '" .$id. "%' ORDER BY compound_type ASC";
    $res = mysql_query($sql_comp);

    if(mysql_num_rows($res)){

        while($comp = mysql_fetch_assoc($res)){

        $compound_type = $comp['compound_type'];
       // $compound_id   = $comp['compound_id'];



        $display .= "<tr>";

            //---display compound_type
            $display .= "<td style='border:none;'>
            <input type='text' name='compound_type[]' value='$compound_type' style='border:none;' size='10' />
           </td>";
            //---input box for output
            $display .= "<td style='border:none;'><input type='text' name='output[]' value='' size='7' /></td>";

            //----i used foreach to loop textbox in every reject acronym.
            foreach($reject_id AS $reject_i)                {
                $display .= "<td style='border:none;'><input type='text' name='reject[]' value='' size='7' /></td>";
            }

            $display .= "</tr>";
        }
    }
 }

?>

 <fieldset>
<legend style="font-size:36px;"> Operator's Output and Reject</legend>
<table>
    <tr>
        <th>Compound</th>
        <th>Output</th>
        <?php
        if(isset($display)){
            echo "$display";
        }
        ?>
</table>
</fieldset>

But the output is:

Compound Output

Thank you

This should do it or be close. Wasn’t 100% sure where you are getting reject_acro fields. Grabbed them from the reject_list.

<?php
ob_start();
include "connection.php";
$id = 'SR130719003';

if(isset($id) && !empty($id)){

    //Build display data BEFORE output
	
    $display = "";
	
	//query to select reject acronym
	$sql = "select DISTINCT reject_acro FROM reject_list";
	$rsd = mysql_query($sql);
	while($rs = mysql_fetch_assoc($rsd)) {
		$reject[] = $rs['reject_acro'];
	}
	
	    							
	//query and build array of output_type with op_number and compound_type as primary keys
	$output_array = array();
    $sql = "SELECT op_number, compound_type, compound_output
	FROM op_output WHERE op_number LIKE '" .$id. "%'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_assoc($rsd)) {
		$output_array[$rs['op_number']][$rs['compound_type']] = $rs['compound_output'];
    }
					
    //query and build array of reject_type and reject with op_number and compound_type as primary keys
	$reject_array = array();
    $sql = "SELECT op_number, compound_type, reject_type, reject
	FROM op_reject WHERE op_number LIKE '" .$id. "%'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_assoc($rsd)) {
		$reject_array[$rs['op_number']][$rs['compound_type']][$rs['reject_type']] = $rs['reject'];
    }
	
	//Start Display	
	if(!isset($reject) || isset($reject) && empty($reject)){
		$display .= "";
	}else{
		//display reject acronym as header
		foreach ($reject as $reject_v){
			$display .= "<th style='border:none;'><input type='text' name='reject_type[]' value='$reject_v' style='border:none; font-weight: bold; ' size='5' /></th>";
		}
		
		$display .= "</tr>";     	
        $display .= "<tr>";
		
        //display reject acronym as header
        foreach ($reject as $reject_v){
            $display .= "<th style='border:none;'><input type='text' name='reject_type[]' value='$reject_v' style='border:none; background-color: transparent; text-align:center; color: #FFF; font-weight: bold; ' size='5' /></th>";
        }

        $display .= "</tr>";

	    //query select compound_type
	    $sql_comp = "SELECT DISTINCT compound_type FROM op_reject WHERE op_number LIKE '" .$id. "%' ORDER BY compound_type ASC";
	    $res = mysql_query($sql_comp);
	
	    if(mysql_num_rows($res)){
	
			while($comp = mysql_fetch_assoc($res)){
			
				$compound_type = $comp['compound_type'];				
				
				$display .= "<tr>";
		
		            //---display compound_type
		            $display .= "<td style='border:none;'>
		            <input type='text' name='compound_type[]' value='$compound_type' style='border:none;' size='10' />
		           </td>";
				
		            //---input box for output	
					   		$outputvalue = (array_key_exists($compound_type,$output_array[$id]) ? "{$output_array[$id][$compound_type]}" : "&nbsp;");
		            $display .= "<td style='border:none;'><input type='text' name='output[]' value='$outputvalue' size='7' /></td>";
		
		            //----i used foreach to loop textbox in every reject acronym.
		            foreach($reject AS $reject_t){
							//check if reject_type KEY is in our $reject_array	
					   		$value = (array_key_exists($reject_t,$reject_array[$id][$compound_type]) ? "{$reject_array[$id][$compound_type][$reject_t]}" : "&nbsp;");
			                $display .= "<td style='border:none;'><input type='text' name='reject[]' value='$value' size='7' /></td>";	
					}
		
				$display .= "</tr>";
	        }
	    }
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<title>Operator's Output and Reject</title>
	</head>
	<body>
		<fieldset>
			<legend style="font-size:36px;"> Operator's Output and Reject</legend>
			<table>
			    <tr>
			        <th>Compound</th>
			        <th>Output</th>
			        <?php
			        if(isset($display)){
			            echo "$display";
			        }
			        ?>
			</table>
		</fieldset>
	</body>
</html>

Thank you… I will try it and i will update you.

I tried your code and it was too close. Uhm…reject_acro that displayed will be the DISTINCT reject_type from op_reject where op_number = ‘SR130719003’.

Thank you so much

You seriously should be migrating over to either the mysqli_* extension or PDO for accessing the database, as the mysql_* extension is deprecated as of version 5.5 of PHP and make use of prepared statements to prevent SQL injection. The rule of thumb is to always assume that any data submitted by the user in any way is dangerous until it has been sanitized.

I tried this:


<?php
ob_start();
include "connection.php";
$id = 'SR130719003';

if(isset($id) && !empty($id)){

    //Build display data BEFORE output

    $display = "";

    //query to select reject acronym
    //$sql = "select DISTINCT reject_acro FROM reject_list";
    $sql = "select DISTINCT reject_type FROM op_reject WHERE op_number = '$id'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_assoc($rsd)) {
        $reject[] = $rs['reject_type'];
    }


    //query and build array of output_type with op_number and compound_type as primary keys
    $output_array = array();
    $sql = "SELECT op_number, compound_type, compound_output
    FROM op_output WHERE op_number LIKE '" .$id. "%'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_assoc($rsd)) {
        $output_array[$rs['op_number']][$rs['compound_type']] = $rs['compound_output'];
    }

    //query and build array of reject_type and reject with op_number and compound_type as primary keys
    $reject_array = array();
    $sql = "SELECT op_number, compound_type, reject_type, reject
    FROM op_reject WHERE op_number LIKE '" .$id. "%'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_assoc($rsd)) {
        $reject_array[$rs['op_number']][$rs['compound_type']][$rs['reject_type']] = $rs['reject'];
    }

    //Start Display
    if(!isset($reject) || isset($reject) && empty($reject)){
        $display .= "";
    }else{
        //display reject acronym as header
        foreach ($reject as $reject_v){
            $display .= "<th style='border:none;'><input type='text' name='reject_type[]' value='$reject_v' style='border:none; font-weight: bold; ' size='5' /></th>";
        }

        $display .= "</tr>";
        $display .= "<tr>";

        //display reject acronym as header
        foreach ($reject as $reject_v){
            $display .= "<th style='border:none;'><input type='text' name='reject_type[]' value='$reject_v' style='border:none; background-color: transparent; text-align:center; color: #FFF; font-weight: bold; ' size='5' /></th>";
        }

        $display .= "</tr>";

        //query select compound_type
        $sql_comp = "SELECT DISTINCT compound_type FROM op_reject WHERE op_number LIKE '" .$id. "%' ORDER BY compound_type ASC";
        $res = mysql_query($sql_comp);

        if(mysql_num_rows($res)){

            while($comp = mysql_fetch_assoc($res)){

                $compound_type = $comp['compound_type'];

                $display .= "<tr>";

                    //---display compound_type
                    $display .= "<td style='border:none;'>
                    <input type='text' name='compound_type[]' value='$compound_type' style='border:none;' size='10' />
                   </td>";

                    //---input box for output
                               $outputvalue = (array_key_exists($compound_type,$output_array[$id]) ? "{$output_array[$id][$compound_type]}" : " ");
                    $display .= "<td style='border:none;'><input type='text' name='output[]' value='$outputvalue' size='7' /></td>";

                    //----i used foreach to loop textbox in every reject acronym.
                    foreach($reject AS $reject_t){
                            //check if reject_type KEY is in our $reject_array
                               $value = (array_key_exists($reject_t,$reject_array[$id][$compound_type]) ? "{$reject_array[$id][$compound_type][$reject_t]}" : " ");
                            $display .= "<td style='border:none;'><input type='text' name='reject[]' value='$value' size='7' /></td>";
                    }

                $display .= "</tr>";
            }
        }
    }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Operator's Output and Reject</title>
    </head>
    <body>
        <fieldset>
            <legend style="font-size:36px;"> Operator's Output and Reject</legend>
            <table>
                <tr>
                    <th>Compound</th>
                    <th>Output</th>
                    <?php
                    if(isset($display)){
                        echo "$display";
                    }
                    ?>
            </table>
        </fieldset>
    </body>
</html>

and it works…

uhmm… instead of insert i need to create an update… should be the same code from insert excep the code for update query?

Thank you

Should be close to what was done on the first example. Be sure to add that extra compound_id key to the form.

<input type='text' name='reject[$compound_id][]' value='$value' size='7' />

…and the hidden field holding this same key.

<input type='hidden' name='compound_id[]' value='$compound_id' /> 

Then you will have the same array to loop through to build update.