Process Multi Record Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nskiran nskiran
    New Member
    • Jan 2012
    • 1

    Process Multi Record Form

    Hi,

    Iam pulling my hair for the below problem since many days!

    1. Display a multi record block in a form through PHP

    2. On click of submit button, form should collect the data from multi record block and store them in DB using mySQL

    If this forum can help on this problem, I am eternally greatful.

    Thanks
    A PHP Lover
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Usually you need to be more specific, give us code and tell us where you're stuck, but here's a quick run down in pseudo-code:

    Code:
    <?php
    
    // get the $records you want to display if any
    
    if($_POST['save']) {
       $db = <create_db_connection>;
       $firstArr = $_POST['first']; 
       $secondArr = $_POST['second'];
    
       // do your validation, if using mysql, use mysql_real_escape_string() function 
       // if validation successfull, loop and save to DB
    
       for($i = 0; isset($firstArr[$i]); $i++) {
           // save value to db
           $db->query("INSERT INTO table SET first = '{$firstArr[$i]}', second = '{$secondArr[$i]}';");
    
       }
    
       // go to next page using header("Location: newpage?status=success");
       // or just display a message
       
    
    }
    ?>
    .
    . HTML HEADERS
    . 
    <form method='post' action='' >
    <table>
    <?php 
       // create record block (assuming $records is an array of objects)
       foreach($records as $rec) {
         echo "<tr><td>";
         echo "<input type='text' name='first[]' value='$rec->first' />";
         echo "</td><td>";
         echo "<input type='text' name='second[]' value='$rec->second' />";
         echo "</td></tr>"; 
       }
    ?>
    </table>
    <input type="submit" name="save" value="Save Records" />
    </form>
    .
    .
    .
    END HTML footer
    Let me know if any questions,


    Dan

    Comment

    Working...