【问题标题】:Treat a lot of (same) inputs in 1 form?以一种形式处理大量(相同)输入?
【发布时间】:2016-10-06 16:18:18
【问题描述】:

我是 PHP-PDO 的新手,我正在尝试学习如何使用类,我想我必须使用它。

所以我正在创建一个管理面板来发布“新闻”,我需要大量输入:

这些数据将用于填充网站主页上的一个数组,以呈现我推荐的“每日投注”。

但我不知道处理这些信息的最佳方式?我曾想过将 <input> 重命名为 name="1"、name="2" 等,但在 MySQL 中注册所有内容将是一场灾难。

有人可能有想法?谢谢 !

【问题讨论】:

    标签: php sql forms input pdo


    【解决方案1】:

    您可以将相同的name 设置为数组的每个字段,例如sports[]。当您提交表单时,这将创建一个包含所有“运动”的数组。

    <input type="text" name="sports[]">
    <input type="text" name="game[]">
    <input type="text" name="prediction[]">
    

    例如,如果您发送足球、足球、棒球、篮球;那么,您的 PHP 将如下所示:

    $sports = $_POST['sports'];
    // now:
    // $sports[0] = 'football'
    // $sports[1] = 'soccer'
    // $sports[2] = 'baseball'
    // $sports[3] = 'basketball'
    

    因此,您所要做的就是循环遍历$sports(以及您拥有的所有输入)以获得相应的值。它按出现的顺序排序。

    foreach($sports as $k => $v){
        $sport = $v; // $k will have the 0, 1, 2, 3 (indexes) if you need them.
                     // $v will have 'football', 'soccer' and so on.
                     // in other words, this loop will run 4 times in my example.
        // you should do SQL stuff here, each item independently (if you need to)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      相关资源
      最近更新 更多