【问题标题】:How to make a new field in table when user clicks on a link用户单击链接时如何在表中创建新字段
【发布时间】:2018-06-10 10:47:24
【问题描述】:

我的表格如下图所示:

为了将此表单中的数据插入表中,我编写了以下代码:

// supplier info
$supplier_name = $_POST['supplier_name'];
$supplier_sending = $_POST['supplier_sending'];
$supplier_guarantee = $_POST['supplier_guarantee'];
$supplier_price = $_POST['supplier_price'];
$supplier_colors = $_POST['supplier_colors'];
$supplier_info = array($supplier_name, 
$supplier_sending,$supplier_guarantee, $supplier_price, $supplier_colors);
$proSupply = json_encode($supplier_info);

通过这种方式,我可以成功地将$proSupply var 提交到我的表中。但正如您在 打印屏幕 中看到的那样,我添加了一个名为 Add more providers: + 的链接,它的作用基本上是向形式。因此用户可以插入多个供应商信息。

但问题是我的表中只有一个字段应该包含供应商信息。而且因为我不知道用户想要添加多少供应商,所以我无法指定 MySQL 表中归档的供应商数量。

所以我的问题是:有没有办法在表中创建一个新的自定义字段,每当用户点击 PHP 中的 + 链接(例如供应商信息_1、供应商信息_2 等)?

更新 1

这是为我的表格提供新行的脚本。

<script>
        $("#addbtn").click(function(){
            var next = parseInt($('#counter').val()) + 1;
            $("#group").append("<table class='table table-hover'>
                                 <tr>
                                     <th style='padding-left:10px'>Name of supplier ("+next+")</th>
                                     <th>Terms of sending</th>
                                     <th>Guarantee</th>
                                     <th>Price</th>
                                     <th>Colors (use , for separation)</th>
                                 </tr>
                                 <tr>
                                     <td style='padding-left:10px'><input name='supplier_name_("+next+")' type='text'></input></td>
                                     <td><input name='supplier_sending_("+next+")' type='text'></input></td>
                                     <td><input name='supplier_guarantee_("+next+")' type='text'></input></td>
                                     <td><input name='supplier_price_("+next+")' type='text'></input></td>
                                     <td><input name='supplier_colors_("+next+")' type='text'></input></td>
                                 </tr>
                                </table>");
            $('#counter').val(next);
        });
        </script>

【问题讨论】:

  • 最好有一个单独的表,每个供应商都有一个新行。
  • 用 user_id 作为 foreign_key 创建单独的表,并添加你想要的供应商(每个供应商的数据在表中的一个新行)
  • 您也可以将数据以json格式保存在该现有字段中。
  • @DeepKakkar 同意了,但是如果他/她想对这些数据进行排序/尝试对这些数据进行一些搜索/或者在不久的将来尝试使用某种 JOIN,将成为非常冗长的任务并且肯定会卡在某个时间点。这就是归一化出现的原因
  • 同意您的评论。 @AlivetoDie

标签: php mysql sql mysqli


【解决方案1】:

是的,我的库存和会计软件中有类似的概念。您可以使用 javascript 或更优选地使用 jquery 来处理它。您可以为供应商创建隐藏行。当基于您的应用程序触发事件时,将创建一个副本以获取另一个供应商行。

【讨论】:

    猜你喜欢
    • 2014-12-10
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多