Inserting multiple rows to database table from a form

MikeOscarEcho

I have a checklist form that managers can fill out. And at the bottom, they type out a material list that their employees can sign off on when complete. The amount of materials can be as long as 20+ rows. Below is what the rows they fill out contain:

        <tr><td><button type="button" class="addRow">Add Row</button></td></tr>
        <tr><th>WO# / Date <br/>(if added or changed after Revision 1)<th>Component Name and Number</th><th>Finish Sizes</th><th>Material</th><th>Total # Pieces</th><th>Work Order</th><th>Notes</th><th>Work Order</th><th>Notes</th><th>Work Order</th><th>Notes</th></tr>
        <tr>
            <td><input type="text" name='wo_num_and_date'/></td>
            <td><input type="text" name='comp_name_and_num'/></td>
            <td><input type="text" name='finish_sizes'/></td>
            <td><input type="text" name='material'/></td>
            <td><input type="text" name='total_num_pieces'/></td>
            <td><input type="text" name='workorder_num_one'/></td>
            <td><textarea rows="2" cols="12" name='notes_one'></textarea></td>
            <td><input type="text" name='workorder_num_two'/></td>
            <td><textarea rows="2" cols="12" name='notes_two'></textarea></td>
            <td><input type="text" name='workorder_num_three'/></td>
            <td><textarea rows="2" cols="12" name='notes_three'></textarea></td>
        </tr>

It starts out with just 1 fillable row and then I've got some jquery that allows them to add more rows.

My insert script below is only inserting the first row in my form.

if(isset($_POST['submit'])){
   $user = getuserinfo($loggedin_id);
   $posted_date = $_POST['posted_date'];
   $revision = $_POST['revision'];
   $per_wo_num = $_POST['per_wo_num'];
   $category = $_POST['category'];

   $wo_num_and_date = $_POST['wo_num_and_date'];
   $comp_name_and_num = $_POST['comp_name_and_num'];
   $finish_sizes = $_POST['finish_sizes'];
   $material = $_POST['material'];
   $total_num_pieces = $_POST['total_num_pieces'];
   $workorder_num_one = $_POST['workorder_num_one'];
   $notes_one = $_POST['notes_one'];
   $workorder_num_two = $_POST['workorder_num_two'];
   $notes_two = $_POST['notes_two'];
   $workorder_num_three = $_POST['workorder_num_three'];
   $notes_three = $_POST['notes_three'];


$sql = "INSERT INTO checklist_revision (job_num, user_id, revision_num, category, posted_date, per_workorder_number)
VALUES ($job_num,
        $loggedin_id,
        $revision,
        $category,
        STR_TO_DATE('$posted_date', '%Y-%m-%d'),
        $per_wo_num);";

 mysqli_query($dbc3, $sql);


$revision_id = mysqli_insert_id($dbc3);

$sql2 = "INSERT INTO checklist_component_stock (revision, job_num, category, posted_date, wo_num_and_date, comp_name_and_number, finish_sizes, material, total_num_pieces, workorder_num_one, notes_one, signoff_user_one, workorder_num_two, notes_two, signoff_user_two, workorder_num_three, notes_three, signoff_user_three)
VALUES ('$revision_id',
        '$job_num',
        '$category',
        STR_TO_DATE('$posted_date', '%Y-%m-%d'),
        '$wo_num_and_date',
        '$comp_name_and_num',
        '$finish_sizes',
        '$material',
        '$total_num_pieces',
        '$workorder_num_one',
        '$notes_one',
        NULL,
        '$workorder_num_two',
        '$notes_two',
        NULL,
        '$workorder_num_three',
        '$notes_three',
        NULL);";

 mysqli_query($dbc3, $sql2);

How do I make it so no matter how many rows they add, it inserts them all into the table instead of just the first row?

VAR DUMP after trying Huseyin's answer:

array (size=17)
  'submit' => string 'Submit!' (length=7)
  'posted_date' => string '2014-04-07' (length=10)
  'category' => string '1' (length=1)
  'revision' => string '4' (length=1)
  'revisionDate' => string '2014-04-07' (length=10)
  'per_wo_num' => string '2' (length=1)
  'wo_num_and_date' => string 'WO#5/2013-04-04' (length=15)
  'comp_name_and_num' => string 'Lift 2' (length=6)
  'finish_sizes' => string '2x2x2' (length=5)
  'material' => string 'P20' (length=3)
  'total_num_pieces' => string '1' (length=1)
  'workorder_num_one' => string '1' (length=1)
  'notes_one' => string 'OK' (length=2)
  'workorder_num_two' => string '2' (length=1)
  'notes_two' => string 'OK' (length=2)
  'workorder_num_three' => string '3' (length=1)
  'notes_three' => string 'NOT OK' (length=6) 
Hüseyin BABAL

You can use array notationed names in form and implement following;

<tr>
    <td><input type="text" name='wo_num_and_date[]'/></td>
    <td><input type="text" name='comp_name_and_num[]'/></td>
    <td><input type="text" name='finish_sizes[]'/></td>
    <td><input type="text" name='material[]'/></td>
    <td><input type="text" name='total_num_pieces[]'/></td>
    <td><input type="text" name='workorder_num_one[]'/></td>
    <td><textarea rows="2" cols="12" name='notes_one[]'></textarea></td>
    <td><input type="text" name='workorder_num_two[]'/></td>
    <td><textarea rows="2" cols="12" name='notes_two[]'></textarea></td>
    <td><input type="text" name='workorder_num_three[]'/></td>
    <td><textarea rows="2" cols="12" name='notes_three[]'></textarea></td>
</tr>

And in php;

    if(isset($_POST['submit'])){
       $user = getuserinfo($loggedin_id);
       $posted_date = $_POST['posted_date'];
       $revision = $_POST['revision'];
       $per_wo_num = $_POST['per_wo_num'];
       $category = $_POST['category'];

       foreach ($_POST['wo_num_and_date'] as $k => $v) {
           $wo_num_and_date = $_POST['wo_num_and_date'][$k];
           $comp_name_and_num = $_POST['comp_name_and_num'][$k];
           $finish_sizes = $_POST['finish_sizes'][$k];
           $material = $_POST['material'][$k];
           $total_num_pieces = $_POST['total_num_pieces'][$k];
           $workorder_num_one = $_POST['workorder_num_one'][$k];
           $notes_one = $_POST['notes_one'][$k];
           $workorder_num_two = $_POST['workorder_num_two'][$k];
           $notes_two = $_POST['notes_two'][$k];
           $workorder_num_three = $_POST['workorder_num_three'][$k];
           $notes_three = $_POST['notes_three'][$k];


        $sql = "INSERT INTO checklist_revision (job_num, user_id, revision_num, category, posted_date, per_workorder_number)
        VALUES ($job_num,
                $loggedin_id,
                $revision,
                $category,
                STR_TO_DATE('$posted_date', '%Y-%m-%d'),
                $per_wo_num);";

         mysqli_query($dbc3, $sql);


        $revision_id = mysqli_insert_id($dbc3);

        $sql2 = "INSERT INTO checklist_component_stock (revision, job_num, category, posted_date, wo_num_and_date, comp_name_and_number, finish_sizes, material, total_num_pieces, workorder_num_one, notes_one, signoff_user_one, workorder_num_two, notes_two, signoff_user_two, workorder_num_three, notes_three, signoff_user_three)
        VALUES ('$revision_id',
                '$job_num',
                '$category',
                STR_TO_DATE('$posted_date', '%Y-%m-%d'),
                '$wo_num_and_date',
                '$comp_name_and_num',
                '$finish_sizes',
                '$material',
                '$total_num_pieces',
                '$workorder_num_one',
                '$notes_one',
                NULL,
                '$workorder_num_two',
                '$notes_two',
                NULL,
                '$workorder_num_three',
                '$notes_three',
                NULL);";

         mysqli_query($dbc3, $sql2);    
       }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inserting multiple rows to database table from a form

From Dev

Inserting multiple rows into a MySQL database from a HTML Form

From Dev

Inserting into Multiple Tables within a database from a form

From Dev

Inserting into Multiple Tables within a database from a form

From Dev

Inserting multiple rows in database

From Dev

Inserting multiple fields from a form into single field in database

From Dev

Insert multiple rows into a MySQL database from a table

From Dev

PHP array not inserting multiple rows to database

From Dev

Inserting Multiple Rows In A SQLITE Database Using qt

From Dev

Inserting multiple rows into the sql database in php

From Dev

PHP array not inserting multiple rows to database

From Dev

Not inserting data form into database table PHP

From Dev

Inserting multiple rows in temp table without loop

From Dev

Insert multiple rows to database from HTML form using MySQLi

From Dev

Taking rows in chunks from datatable and inserting in database

From Dev

Rails 4.1.6 inserting not saving in database from form

From Dev

INSERT into database from Form not inserting but producing no errors

From Dev

Inserting today date from form to database

From Dev

Inserting data from a html form into MySQL database

From Dev

INSERT into database from Form not inserting but producing no errors

From Dev

Inserting special characters into SQL database from form

From Dev

Updating Multiple SQL rows from HTML Table/Form with PHP

From Dev

Inserting rows from one table to other jquery

From Dev

Inserting rows from one table to other jquery

From Dev

Inserting from one database table to another

From Dev

Inserting from one database table to another

From Dev

Inserting rows in a table from another table using a third table

From Dev

inserting multiple values against multiple ids in database table

From Dev

Scala Slick inserting multiple random rows into PostgreSql database

Related Related

  1. 1

    Inserting multiple rows to database table from a form

  2. 2

    Inserting multiple rows into a MySQL database from a HTML Form

  3. 3

    Inserting into Multiple Tables within a database from a form

  4. 4

    Inserting into Multiple Tables within a database from a form

  5. 5

    Inserting multiple rows in database

  6. 6

    Inserting multiple fields from a form into single field in database

  7. 7

    Insert multiple rows into a MySQL database from a table

  8. 8

    PHP array not inserting multiple rows to database

  9. 9

    Inserting Multiple Rows In A SQLITE Database Using qt

  10. 10

    Inserting multiple rows into the sql database in php

  11. 11

    PHP array not inserting multiple rows to database

  12. 12

    Not inserting data form into database table PHP

  13. 13

    Inserting multiple rows in temp table without loop

  14. 14

    Insert multiple rows to database from HTML form using MySQLi

  15. 15

    Taking rows in chunks from datatable and inserting in database

  16. 16

    Rails 4.1.6 inserting not saving in database from form

  17. 17

    INSERT into database from Form not inserting but producing no errors

  18. 18

    Inserting today date from form to database

  19. 19

    Inserting data from a html form into MySQL database

  20. 20

    INSERT into database from Form not inserting but producing no errors

  21. 21

    Inserting special characters into SQL database from form

  22. 22

    Updating Multiple SQL rows from HTML Table/Form with PHP

  23. 23

    Inserting rows from one table to other jquery

  24. 24

    Inserting rows from one table to other jquery

  25. 25

    Inserting from one database table to another

  26. 26

    Inserting from one database table to another

  27. 27

    Inserting rows in a table from another table using a third table

  28. 28

    inserting multiple values against multiple ids in database table

  29. 29

    Scala Slick inserting multiple random rows into PostgreSql database

HotTag

Archive