doujia1939 2017-02-16 21:47
浏览 49
已采纳

动态属性和值保存到数据库

I have a database table like :

id | title | Url

And i have a form like the screenshot...

enter image description here

i have tried like that

<div class="col-md-6">
  <input type="text" class="form-control " style="border-radius: 0px;" placeholder="Tittle" id="slider" name="title[]">
</div>
<div class="col-md-6">
  <input type="text" class="form-control col-md-6" style="border-radius: 0px;" placeholder="Link" id="slider" name="url[]">
</div>

And the submission result is :

Array
(
    [0] => Array
        (
            [title] => test1
        )

    [1] => Array
        (
            [url] => #1
        )

    [2] => Array
        (
            [title] => test2
        )

    [3] => Array
        (
            [url] => #2
        )

    [4] => Array
        (
            [title] =>title3
        )

    [5] => Array
        (
            [url] => #3
        )

)

The problem is i cannot filter which url for the title1,and which for the title 2 etc..

Is there any way to have the result like this array?

Array
(
    [0] => Array
        (
            [title] => test1,
            [url] => #1

        )

    [1] => Array
        (
            [title] => test2,
            [url] => #2
        )

    [2] => Array
        (
            [title] => test3,
            [url] => #3
        )


  )
  • 写回答

1条回答 默认 最新

  • dongnao2582 2017-02-16 22:21
    关注

    The name of each field should end with [] For example

    <input type="text" class="form-control " style="border-radius: 0px;" placeholder="Tittle" id="slider" name="titles[name][]">
    

    This will make them available in an array in your PHP script, which you can then just iterate over, forming your query.

      $Q = 'insert into mytable (name, value) values ';
      $V = [];
    
      foreach ($titles as $idx => $title) {
        $V[] = "('".$title['name']."','".$title['value']."')";
      }
      $Q.= implode(',', $V);
    

    [EDIT]

    OK, make your fields look something like this

    <INPUT TYPE=TEXT NAME="data[title][]">
    <INPUT TYPE=TEXT NAME="data[url][]">
    

    Then output it and see what you've got, and the PHP code above should help you form your query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答