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 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制