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> PHP: 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.
Without knowing what error you get or what debugging you have already done I'm going to take a quick stab at it and guess that the sql isn't working. You have $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]')"; PHP: but the array values won't be inserted into the string nicely unless you have it like $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]}')"; PHP: putting the curly brackets around the variable tells it that you want to save $compound_type[$s] and not $compound_type.
Hi, I tried this : $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++) { //i got problem here 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); } } } } } PHP: 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
here is the output of var_dump. array(9) { [0]=> string(2) "SC" [1]=> string(2) "SP" [2]=> string(2) "SH" [3]=> string(2) "PG" [4]=> string(2) "PT" [5]=> string(2) "PS" [6]=> string(1) "C" [7]=> string(1) "M" [8]=> string(1) "R" } 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 ($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($x = 0; $x < count($compound_type) ; $x++) for($i = 0; $i < count($reject_type) ; $i++) { if($reject[$x] != "") { $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); } 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> PHP: here is my sample data testing: Compound Type---Output---SC---SP---SH--- P28--------------10-------------1-----2 P32--------------20--------5--------------- P32NW--------------------------3---------- and the output save is: 1 001 P32 SP 1.00 2 001 P32NW SH 2.00 3 001 P32 SP 1.00 4 001 P32NW SH 2.00 5 001 P32 SP 1.00 6 001 P32NW SH 2.00 but is should be: op_id--op_number---compound_type----reject_type---reject 1------001------------P28-----------------SP------------1 2------001------------P28-----------------SH------------2 3------001------------P32-----------------SC------------5 4------001------------P32NW-------------SP------------3 Thank you so much
I also tried this: //----code to save reject data---// for($i = 0; $i < count($reject_type) ; $i++) { for($x = 0; $x < count($compound_type[$i]) ; $x++) { if($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 { } } } PHP: and the output is: 1 001 P28 SP 1.00 2 001 P28 SH 2.00 Only P28 was save.
Don't forget to use the {} brackets - they really do matter. if you echo out each $sql_re onto your screen and then run them manually through phpMyAdmin do you get any error notifications?
I add the {}. this is the output of echo $sql_re INSERT INTO op_reject (op_number, compound_type, reject_type, reject) VALUES ('001','P28', 'SP', '1') Thank you.
No... It's just that was display when I echo $rsql_re: 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]}')"; echo $sql_re; exit; $result_re = mysql_query($sql_re, $con); } else { } } } PHP: ONly one insert query displayed.
I think it's my loop was a problem and until now I can't figured out Thank you for your help and support.
got it, line 4 should be if(!empty($reject[$x])) but it would be altogether simpler as if (count($reject_type)) { foreach ($reject_type as $k => $var_reject) { echo "<li>{$k} - {$var_reject}"; if (!empty($var_reject)) { if (count($compound_type)) { foreach ($compound_type as $var_ct) { echo "<li>{$var_ct}"; $sql_re = "INSERT INTO `op_reject` (`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','{$var_ct}', '{$var_reject}', '{$reject_type[$k]}')"; echo "<li>$sql_re"; mysql_query($sql_re, $con); } } } } } PHP: Once it's up and running strip out the echo's but you should be able to see it stepping through the script and identify why it stops.
The output of this code are: 0 - SC P28 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P28', 'SC', 'SC') P32 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32', 'SC', 'SC') P32NW INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32NW', 'SC', 'SC') P33 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P33', 'SC', 'SC') P35 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35', 'SC', 'SC') P35M INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35M', 'SC', 'SC') P35P INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35P', 'SC', 'SC') P38 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P38', 'SC', 'SC') P40 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P40', 'SC', 'SC') P41 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P41', 'SC', 'SC') P42 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P42', 'SC', 'SC') P43 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P43', 'SC', 'SC') P46 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P46', 'SC', 'SC') P47 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P47', 'SC', 'SC') 1 - SP P28 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P28', 'SP', 'SP') P32 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32', 'SP', 'SP') P32NW INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32NW', 'SP', 'SP') P33 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P33', 'SP', 'SP') P35 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35', 'SP', 'SP') P35M INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35M', 'SP', 'SP') P35P INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35P', 'SP', 'SP') P38 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P38', 'SP', 'SP') P40 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P40', 'SP', 'SP') P41 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P41', 'SP', 'SP') P42 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P42', 'SP', 'SP') P43 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P43', 'SP', 'SP') P46 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P46', 'SP', 'SP') P47 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P47', 'SP', 'SP') 2 - SH P28 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P28', 'SH', 'SH') P32 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32', 'SH', 'SH') P32NW INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32NW', 'SH', 'SH') P33 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P33', 'SH', 'SH') P35 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35', 'SH', 'SH') P35M INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35M', 'SH', 'SH') P35P INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35P', 'SH', 'SH') P38 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P38', 'SH', 'SH') P40 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P40', 'SH', 'SH') P41 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P41', 'SH', 'SH') P42 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P42', 'SH', 'SH') P43 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P43', 'SH', 'SH') P46 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P46', 'SH', 'SH') P47 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P47', 'SH', 'SH') 3 - PG P28 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P28', 'PG', 'PG') P32 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32', 'PG', 'PG') P32NW INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32NW', 'PG', 'PG') P33 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P33', 'PG', 'PG') P35 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35', 'PG', 'PG') P35M INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35M', 'PG', 'PG') P35P INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35P', 'PG', 'PG') P38 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P38', 'PG', 'PG') P40 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P40', 'PG', 'PG') P41 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P41', 'PG', 'PG') P42 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P42', 'PG', 'PG') P43 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P43', 'PG', 'PG') P46 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P46', 'PG', 'PG') P47 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P47', 'PG', 'PG') 4 - PT P28 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P28', 'PT', 'PT') P32 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32', 'PT', 'PT') P32NW INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32NW', 'PT', 'PT') P33 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P33', 'PT', 'PT') P35 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35', 'PT', 'PT') P35M INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35M', 'PT', 'PT') P35P INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35P', 'PT', 'PT') P38 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P38', 'PT', 'PT') P40 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P40', 'PT', 'PT') P41 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P41', 'PT', 'PT') P42 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P42', 'PT', 'PT') P43 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P43', 'PT', 'PT') P46 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P46', 'PT', 'PT') P47 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P47', 'PT', 'PT') 5 - PS P28 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P28', 'PS', 'PS') P32 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32', 'PS', 'PS') P32NW INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32NW', 'PS', 'PS') P33 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P33', 'PS', 'PS') P35 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35', 'PS', 'PS') P35M INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35M', 'PS', 'PS') P35P INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35P', 'PS', 'PS') P38 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P38', 'PS', 'PS') P40 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P40', 'PS', 'PS') P41 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P41', 'PS', 'PS') P42 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P42', 'PS', 'PS') P43 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P43', 'PS', 'PS') P46 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P46', 'PS', 'PS') P47 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P47', 'PS', 'PS') 6 - C P28 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P28', 'C', 'C') P32 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32', 'C', 'C') P32NW INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32NW', 'C', 'C') P33 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P33', 'C', 'C') P35 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35', 'C', 'C') P35M INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35M', 'C', 'C') P35P INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35P', 'C', 'C') P38 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P38', 'C', 'C') P40 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P40', 'C', 'C') P41 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P41', 'C', 'C') P42 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P42', 'C', 'C') P43 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P43', 'C', 'C') P46 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P46', 'C', 'C') P47 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P47', 'C', 'C') 7 - M P28 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P28', 'M', 'M') P32 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32', 'M', 'M') P32NW INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32NW', 'M', 'M') P33 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P33', 'M', 'M') P35 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35', 'M', 'M') P35M INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35M', 'M', 'M') P35P INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35P', 'M', 'M') P38 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P38', 'M', 'M') P40 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P40', 'M', 'M') P41 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P41', 'M', 'M') P42 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P42', 'M', 'M') P43 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P43', 'M', 'M') P46 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P46', 'M', 'M') P47 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P47', 'M', 'M') 8 - R P28 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P28', 'R', 'R') P32 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32', 'R', 'R') P32NW INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P32NW', 'R', 'R') P33 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P33', 'R', 'R') P35 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35', 'R', 'R') P35M INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35M', 'R', 'R') P35P INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P35P', 'R', 'R') P38 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P38', 'R', 'R') P40 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P40', 'R', 'R') P41 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P41', 'R', 'R') P42 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P42', 'R', 'R') P43 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P43', 'R', 'R') P46 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P46', 'R', 'R') P47 INSERT INTO `op_reject`(`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','P47', 'R', 'R')
that's pretty much what I'd expect, a huge number of records... is that what you want? I don't get why you have the second run through of compound type. I'd have expected this version where you are just matching the 3 arrays with each other if (count($reject_type)) { foreach ($reject_type as $k => $var_reject) { echo "<li>{$k} - {$var_reject}"; if (!empty($var_reject)) { echo "<li>{$var_ct}"; $sql_re = "INSERT INTO `op_reject` (`op_number`, `compound_type`, `reject_type`, `reject`) VALUES ('001','{$compound_type[$k]}', '{$var_reject}', '{$reject_type[$k]}')"; echo "<li>$sql_re"; mysql_query($sql_re, $con); } } } PHP:
try that last bit of code I posted, should fit better. Your earlier version was matching all rejects to all compound types so instead of getting 1-1-1 2-2-2 3-3-3 you were getting 1-1-1 1-1-2 1-1-3 1-2-1 1-2-2 1-2-3 and so on