【问题标题】:Add HTML dynamic table values Into Single INSERT statement将 HTML 动态表值添加到单个 INSERT 语句中
【发布时间】:2013-11-03 11:37:40
【问题描述】:

这是我的主页,它在表中添加动态行(jQuery 正在执行此操作),因为我有 'n' 行要添加,我如何为所有动态行数执行 SQL 插入语句(有 4不同的文本字段)。

在 Simple 中,我有 - (1,2,3,x,y,z) 元素作为 Insert 语句值,其中 x,y,z 可以是任意数量的行(但每次 x,y,z 都会是相等数量的元素),连同 1,2,3 我需要添加这个 x,y,z 数组。插入语句中的 FOR 循环是我从 google n stackOverflow 得到的,但是你如何 FOR 循环 4 个不同的文本字段以及 1、2、3(预定义元素)

更简单-

(value1,value2,value3,x,y,z);
(value1,value2,value3,a,b,c);
(value1,value2,value3,e,f,g);
(value1,value2,value3,q,w,e);
(value1,value2,value3,a,s,d);

你如何在 MySQL 单 INSERT 语句中插入上面的这个?

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
        <script src="jquery.json-2.4.min.js"></script>
        <script>
        $(document).ready(function() {
            var id = 0;

            // Add button functionality
            $("table.dynatable button.add").click(function() {
                id++;
                var master = $(this).parents("table.dynatable");

                // Get a new row based on the prototype row

                var prot1= master.find(".prototype").clone();
                prot1.attr("class", "")
                prot1.find(".id").attr("value", id);

                master.find("tbody").append(prot1);
                alert(id);
            });

            // Remove button functionality
            $("table.dynatable button.remove").live("click", function() {
                $(this).parents("tr").remove();

            });

            //Submit button clicked
            $('#button').click(function() {    
                //$("table tr").has("th").remove();
            var data = {
                value : []
            }
            $("table tbody tr").each(function() {

            data.value.push($('.id', this).val());
            data.value.push($('.WID', this).val());
            data.value.push($('.ac', this).val());
            data.value.push($('.tm', this).val());
            data.value.push($('.cutt', this).val());

        });

            console.log(data);
            var id = data.value.join(','); alert(id);

            var myarr1={final:data};

            var myarr2=JSON.stringify(myarr1);
            var myarr3="receivearray.php?url=" + encodeURIComponent(myarr2);

            $('#myFrame1').attr("src", myarr3);
        }); 

        });


        </script>
        <style>
            .dynatable {
                border: solid 1px #000; 
                border-collapse: collapse;
            }
            .dynatable th,
            .dynatable td {
                border: solid 1px #000; 
                padding: 2px 10px;
                width: 170px;
                text-align: center;
            }
            .dynatable .prototype {

            }
        </style>
    </head>
    <body>


            <table id="MyTable" class="dynatable">
            <thead>
                <tr>
                    <th>WID</th>
                    <th>InchargeName</th>
                    <th>Action</th>
                    <th>Time</th>
                    <th><button class="add">Add</button></th>
                </tr>
            </thead>
            <tbody>
                <tr class="prototype" id="pro">
                    <td><input type="text"  name="id[]" value=""  class="id" /></td>
                    <td><input type="text"  name="name[]" value="" class="WID"/></td>
                    <td><input type="text"  name="col4[]" value=""   class="ac" /></td>
                    <td><input type="text"  name="col3[]" value=""  class="tm"/></td>


                    <td><button class="remove">Remove</button>
                </tr>
        </table>


        <input type="button" id="button" value="submit"/>
    </body>
</html>

【问题讨论】:

  • 关于“其中 x,y,z 可以是任意数量的行(但每次 x,y,z 将是相同数量的元素)”的第二部分我完全不清楚。请尝试澄清您到底需要什么,但请记住,我们不是您,并且您的任务的明显(对您)方面对于将阅读此内容的 PPL 是未知的。
  • 我有一个带有添加新行按钮的表格。这一行有 4 个文本字段,所以每次您点击添加时,都会在我的页面中添加一行包含 4 个字段的行,当我点击提交时,所有应添加行以及外部表格元素。
  • 前。 id,name,field 1,field 2,field 3. 每次你点击 ADD 按钮时,field 1,field 2,field 2 将添加为 AJAX 行,比如说,在提交时你现在正在添加 (1,2,x1, y1,z1)(1,2,x2,y3,z3)........(1,2,xn,yn,zn)
  • 代码太多,无法阅读
  • 我得到了@Rolf 的答案,将 for 循环准确地放到了 insert 语句中,仅此而已!!!

标签: php jquery mysql arrays


【解决方案1】:

这会在一个 (ajax) http 请求中发送所有数据:

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
    <script src="jquery.json-2.4.min.js"></script>
    <script>
        $(document).ready(function() {
            var id = 0;

            // Add button functionality
            $("table.dynatable button.add").click(function() {
                id++;
                var master = $(this).parents("table.dynatable");

                // Get a new row based on the prototype row

                var prot1= master.find(".prototype").clone();
                prot1.attr("class", "")
                prot1.find(".id").attr("value", id);

                master.find("tbody").append(prot1);
                alert(id);
            });

            // Remove button functionality
            $("table.dynatable button.remove").live("click", function() {
                $(this).parents("tr").remove();

            });

            //Submit button clicked
            $('#button').click(function() {
                //$("table tr").has("th").remove();
                var data = {
                    final:{
                        value : []
                    }
                }
                $("table tbody tr").each(function() {
                    // create an array of objects
                    data.final.value.push({
                        id: $('.id', this).val(),
                        wid: $('.WID', this).val(),
                        ac: $('.ac', this).val(),
                        tm: $('.tm', this).val(),
                        cutt: $('.cutt', this).val()
                    });
                });

                console.log(eval(data));

                $.ajax({
                    async: false,
                    url: 'receivearray.php',
                    dataType: 'json',
                    data: data,
                    type: 'get'
                });

                $('#myFrame1').attr("src", myarr3);
            });

        });


    </script>
    <style>
        .dynatable {
            border: solid 1px #000;
            border-collapse: collapse;
        }
        .dynatable th,
        .dynatable td {
            border: solid 1px #000;
            padding: 2px 10px;
            width: 170px;
            text-align: center;
        }
        .dynatable .prototype {

        }
    </style>
</head>
<body>


<table id="MyTable" class="dynatable">
    <thead>
    <tr>
        <th>WID</th>
        <th>InchargeName</th>
        <th>Action</th>
        <th>Time</th>
        <th><button class="add">Add</button></th>
    </tr>
    </thead>
    <tbody>
    <tr class="prototype" id="pro">
        <td><input type="text"  name="id[]" value=""  class="id" /></td>
        <td><input type="text"  name="name[]" value="" class="WID"/></td>
        <td><input type="text"  name="col4[]" value=""   class="ac" /></td>
        <td><input type="text"  name="col3[]" value=""  class="tm"/></td>


        <td><button class="remove">Remove</button>
    </tr>
</table>


<input type="button" id="button" value="submit"/>
</body>
</html>

这里receivearray.php

<?php
file_put_contents('/tmp/temp.txt',var_export($_REQUEST['final'],true));

【讨论】:

  • 嗨,Ivan,没有 AJAX 会接收数组页面。
  • 对不起,我不明白,你不会使用 ajax 发送数据,或者出现一些错误,ajax 不起作用?
  • 在外面得到了答案,无论如何谢谢伊万
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 1970-01-01
  • 2012-07-16
  • 2016-06-06
  • 2023-04-07
  • 2011-06-18
相关资源
最近更新 更多