dongqing6755 2016-02-25 01:32
浏览 28

将项目列表从HTML表单发布到PHP

What I am trying to achieve is to have an undefined number of input fields (users are able to add additional input fields with a plus-button on the site). Each input field will have their id/name appended with an increasing number. This is already working fine.

Example:

<input type="text" name="field1" id="field1">
<input type="text" name="field2" id="field2">
<input type="text" name="field3" id="field3">
<button id="addanotherinputfield">+ Add new</button>

So in theory, there could be well more than 200 input fields, the last one then being:

<input type="text" name="field200" id="field200">

I currently push this as $_POST (code simplified):

<?php
    $db = new PDO("mysql:host=localhost;dbname=$dbname", $dbtable, $dbpass);
    $sdh = $db->prepare('INSERT INTO yeehaw (name,value) VALUES (:name,:value)');
    if($_POST['field1'] !='') {
        $name = 'field1';
        $value = $_POST['field1'];
        $sdh->bindParam(':name', $name);
        $sdh->bindParam(':value', $value);
        $sdh->execute();
    }
    if($_POST['field2'] !='') {
        $name = 'field2';
        $value = $_POST['field2'];
        $sdh->bindParam(':name', $name);
        $sdh->bindParam(':value', $value);
        $sdh->execute();
    }
    if($_POST['field3'] !='') {
        $name = 'field3';
        $value = $_POST['field3'];
        $sdh->bindParam(':name', $name);
        $sdh->bindParam(':value', $value);
        $sdh->execute();
    }
    $db = null;
?>

Currently this is working as well.

But for obvious reasons this is not a good way to do it, since it's not scalable (I have to add each case to the code and I like to clean out unnecessary code). I have googled for quit a bit now and can't seem to find the answers to two basic questions (maybe I am looking for the wrong terms/terminology?):

  1. How can I make my PHP scale? So it doesn't matter if there is 1 or 1.000.000 input fields without having to list each case in the code (like a loop/foreach)?
  2. Would it be better to open and close a DB connection for every case or do it like my example above? Isn't there a way to open one connection and push all added information at once? How would I go about this?

Any pointers/helpers much appreciated.

  • 写回答

2条回答 默认 最新

  • douqianbiao4216 2016-02-25 01:41
    关注

    HTML

    Set the name attribute of your HTML input to be an array to PHP:

    <input type="text" name="field[]" />
    

    this way the fields will post as an array an you can easily loop over every field in PHP.

    PHP

    foreach($_POST['field'] as field){
        // handle input
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器