从表单中将多行插入数据库表

时间:2014-04-07 12:08:40

标签: php mysql sql

我有一份经理可以填写的清单表格。在底部,他们列出了一份材料清单,员工可以在完成时签字。材料的数量可以长达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>

它首先只有一个可填写的行,然后我有一些jquery允许他们添加更多的行。

下面的我的插入脚本只是在表单中插入第一行。

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

我如何制作它,无论它们添加了多少行,它都将它们全部插入到表格而不是第一行?

在尝试了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 个答案:

答案 0 :(得分:2)

您可以在表单中使用数组标注名称并实现以下内容;

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

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

答案 1 :(得分:1)

这是一个简单的例子:

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

答案 2 :(得分:0)

您应该删除;,如

* 您的代码:( INSERT INTO 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);";

您应该改变:

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

* 您的代码:( INSERT INTO checklist_component_stock)*

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

你应该这样改变:

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