草庐IT

php - 从表单向数据库表插入多行

coder 2023-10-21 原文

我有一个管理人员可以填写的核对表。在底部,他们输入了一份 Material list ,他们的员工可以在完成后签字。 Material 的数量可以长达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>

它从只有 1 个可填充行开始,然后我有一些 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) 

最佳答案

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

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

关于php - 从表单向数据库表插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22912006/

有关php - 从表单向数据库表插入多行的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby-on-rails - 如何在 ruby​​ 交互式 shell 中有多行? - 2

    这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式ruby​​shell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f

  4. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  5. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  6. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  7. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  8. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  9. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  10. ruby-on-rails - 创建 ruby​​ 数据库时惰性符号绑定(bind)失败 - 2

    我正在尝试在Rails上安装ruby​​,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf

随机推荐