【问题标题】:Saving data with dynamic input name to database?将具有动态输入名称的数据保存到数据库?
【发布时间】:2017-06-05 09:11:10
【问题描述】:

好的,在此之前我是 PHP 的初学者。 所以请原谅我这个愚蠢的问题。

假设 id_user '1' 有 2 个项目,因此它将为每个项目创建 2 个输入表单。

这些是输入形式:

<input value="0" type="text" name="store_<?php echo $id= "I got this value from id.item in database, in this case '5'" ?>" required="required">

<input value="0" type="text" name="take_<?php echo $id= "I got this value from id.item in database, in this case '5'" ?>" required="required"> 

<input value="0" type="text" name="store_<?php echo $id= "I got this value from id.item in database, in this case '6'" ?>" required="required"> 

<input value="0" type="text" name="take_<?php echo $id= "I got this value from id.item in database, in this case '6'" ?>" required="required">

注意:值 take_{id} 和 store_{id} 是从数据库中的表 'item' 列 'id_item' 中检索的。

用户输入这些值:

*value 'store' for id_item 5 is 3
*value 'take' for id_item 5 is 2
*value 'store' for id_item 6 is 1
*value 'take' for id_item 6 is 1

如何将用户输入中的“存储”和“获取”值及其关联 ID 保存到数据库? 考虑到 1 个用户可以拥有多个项目。

这是表格项。

    ->id_user (referencing id_user from user's table)
    ->id_item # this primary key
    ->Take
    ->Store

所以最终的结果会是这样的:

id_user : 1
id_item : 5
Store : 3
Take : 2

像这样

id_user : 1
id_item : 6
Store : 1
Take : 1

我整天都在谷歌上搜索,我认为模式为 'store_' 和 'take_' 的正则表达式应该这样做吗?还是有其他方法可以做到这一点?
谢谢你:)

【问题讨论】:

  • 使用数组会不会更容易,例如:&lt;input value="0" type="text" name="store[&lt;?= $id; ?&gt;] ... /&gt; - 那你就可以foreach($_POST['store'] as $id =&gt; $value) { ... }
  • 你想举个例子吗? @CD001

标签: php mysql regex


【解决方案1】:
Set You input like this 
<input value="0" type="text" name="store[]" required="required">

<input value="0" type="text" name="take[]" required="required"> 

<input value="0" type="text" name="store[]" required="required"> 

<input value="0" type="text" name="take[]" required="required">

  and get this value on backside like this   
  $postdata=$_POST;   
  $count= $postdata['store'];  
  for($i=0;$i<$count;$i++){   
       echo $postdata['store'][$i];   
       echo $postdata['take'][$i];   
  }   
 die();    
 see your store and take value 

【讨论】:

  • 好吧,这似乎是一个无限循环。
  • 取决于用户。有多少用户拥有物品。如果用户有 1 个项目,那么它将创建 2 个输入,一个用于存储项目的新值,另一个用于获取它。
【解决方案2】:

假设您正在从 PHP 循环中生成 HTML &lt;input ... /&gt; 字段,例如:

foreach($dbResult as $item) {
    echo "<input type=\"text\" name=\"store[{$item->getId()}]\" />\n"
        . "<input type=\"text\" name=\"take[{$item->getId()}]\" />\n";
}


哪个应该给你一个像这样的$_POST 数组(在你的例子中使用用户输入值):

Array
(
    [store] => Array
        (
            [5] => 3
            [6] => 1
        )

    [take] => Array
        (
            [5] => 2
            [6] => 1
        )

)

其中storetake 数组键是项目ID

然后,您可以使用任一数组中的键生成循环并将数据插入数据库(以 PDO 为例 - 需要在实时环境中处理错误):

if(!empty($_POST['store'])) {

    // we can use the keys from just the `store` array
    // as they'll be the same as those in the `take` array
    foreach(array_keys($_POST['store']) as $itemId) {

        $userId = $_SESSION['user_id']; // from the session for example...
        $storeValue = $_POST['store'][$itemId];
        $takeValue = $_POST['take'][$itemId];

        $qry = "INSERT INTO item(item.id_user, item.id_item, item.store, item.take) "
            . "VALUES(:user_id, :item_id, :store_value, :take_value)";

        $stmt = $db->prepare($qry);
        $stmt->bindParam(':user_id', $userId, PDO::PARAM_INT);
        $stmt->bindParam(':item_id', $itemId, PDO::PARAM_INT);
        $stmt->bindParam(':store_value', $storeValue, PDO::PARAM_INT);
        $stmt->bindParam(':take_value', $takeValue, PDO::PARAM_INT);
        $stmt->execute();
    }
}

【讨论】:

    【解决方案3】:

    经过几个小时的搜索,我终于找到了答案
    我决定对输入表单进行一些更改。

    <input type="text" name="<?php this is id_item?>[0]"> // this array [0] will be assign for 'store' section
    
    <input type="text" name="<?php this is id_item?>[1]"> // this array [1] will be assign for 'take' section
    

    我还添加了 1 个 input type=hidden 用于检索 id_item

    <input type="hidden" name="<?php this is id_item ?>[2]" value="<?php this is id_item ?>"> //this array [2] will be assign for send 'id_item' value.
    

    并循环内容以接收数据

        for($i=0;$i<How much row i have;$i++){
            $store=intval($my_array[$i][0]);
            $take=intval($my_array[$i][1]);
            $amount=$store-$take;
            $id_item=$my_array[$i][2];
            /////////////////////////////////////
            And do insert command to database right here
    

    我知道这不是很漂亮,但它会完成这项工作。

    感谢您对我的问题的回答和评论:)

    【讨论】:

      猜你喜欢
      • 2016-05-05
      • 1970-01-01
      • 2016-03-23
      • 2019-08-20
      • 2018-03-21
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      相关资源
      最近更新 更多