从窗体插入多个行到数据库表

时间:2022-09-15 16:25:54

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:

我有一张管理人员可以填写的检查表。在底部,他们打印出一份材料清单,员工可以在完成后签字。材料的数量可以长达20多行。下面是他们填写的行:

        <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.

它开始时只有1行可填行,然后我有一些jquery允许它们添加更多的行。

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:

试过Huseyin的回答后,VAR DUMP:

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) 

3 个解决方案

#1


2  

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;

在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);    
       }
}

#2


1  

Here is a simple example:

这里有一个简单的例子:

<form action="" method='post'>
    <input type="text" name="myData[row1]" value='aaa'>
    <input type="text" name="myData[row2]" value='sss'>
    <input type="text" name="myData[row3]" value='ddd'>

    <input type="submit" name="submit">
</form>

if(isset($_POST['submit']))
{
    $array = $_POST['myData'];

    echo "<pre>";
    echo print_r($array);
    echo "</pre>";

    //insert 3 times,
    foreach ($array as $key => $value) {
        $sql = "INSERT INTO table1 (info) VALUES('$value')";
    }
}

OUTPUT:
Array
(
    [row1] => aaa
    [row2] => sss
    [row3] => ddd
)

#3


0  

you should remove the ; like as

你应该移除;像

*your code: (INSERT INTO checklist_revision)*

*您的代码:(插入checklist_revision)*

VALUES ($job_num,
        $loggedin_id,
        $revision,
        $category,
        STR_TO_DATE('$posted_date', '%Y-%m-%d'), -- -> you should not give like this you should mention the `colname`
        $per_wo_num);";

your should change like:

你应该改变:

VALUES ($job_num,
        $loggedin_id,
        $revision,
        $category,
        STR_TO_DATE('$posted_date', '%Y-%m-%d'),
        $per_wo_num)";

*your code:(INSERT INTO checklist_component_stock)*

*您的代码:(插入checklist_component_stock)*

 NULL,
    '$workorder_num_three',
    '$notes_three',
    NULL);";

you should change like this:

你应该这样改变:

 NULL,
    '$workorder_num_three',
    '$notes_three',
    NULL)";

#1


2  

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;

在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);    
       }
}

#2


1  

Here is a simple example:

这里有一个简单的例子:

<form action="" method='post'>
    <input type="text" name="myData[row1]" value='aaa'>
    <input type="text" name="myData[row2]" value='sss'>
    <input type="text" name="myData[row3]" value='ddd'>

    <input type="submit" name="submit">
</form>

if(isset($_POST['submit']))
{
    $array = $_POST['myData'];

    echo "<pre>";
    echo print_r($array);
    echo "</pre>";

    //insert 3 times,
    foreach ($array as $key => $value) {
        $sql = "INSERT INTO table1 (info) VALUES('$value')";
    }
}

OUTPUT:
Array
(
    [row1] => aaa
    [row2] => sss
    [row3] => ddd
)

#3


0  

you should remove the ; like as

你应该移除;像

*your code: (INSERT INTO checklist_revision)*

*您的代码:(插入checklist_revision)*

VALUES ($job_num,
        $loggedin_id,
        $revision,
        $category,
        STR_TO_DATE('$posted_date', '%Y-%m-%d'), -- -> you should not give like this you should mention the `colname`
        $per_wo_num);";

your should change like:

你应该改变:

VALUES ($job_num,
        $loggedin_id,
        $revision,
        $category,
        STR_TO_DATE('$posted_date', '%Y-%m-%d'),
        $per_wo_num)";

*your code:(INSERT INTO checklist_component_stock)*

*您的代码:(插入checklist_component_stock)*

 NULL,
    '$workorder_num_three',
    '$notes_three',
    NULL);";

you should change like this:

你应该这样改变:

 NULL,
    '$workorder_num_three',
    '$notes_three',
    NULL)";