Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    712
    Thanks
    165
    Thanked 0 Times in 0 Posts

    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:

    PHP 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.
    Attached Files Attached Files

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Are you seeing errors? What behavior are you seeing? How is it not working?

    Also this:

    PHP Code:
     foreach ($reject as $reject
    Blows away the original array $reject, which is ok I guess, if you know for sure you'll never need it again, but why take that chance? Use a different variable in the foreach:

    PHP Code:
     foreach ($reject as $rejectVal
    And, you should be escaping your inputs before using them in a query, unless you want to get SQL injected? (see mysql_real_escape_string() in the manual)

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    What happens when you echo or print_r $save ? Please post the results.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •