使用动态输入名称将数据保存到数据库?

时间:2017-06-05 09:11:10

标签: php mysql regex

好吧,在那之前,我是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">

注意:从表格&#39; item&#39;中检索值take_ {id}和store_ {id} coloumn&#39; id_item&#39;在数据库中。

用户输入以下值:

*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

如何节省价值&#39;存储&#39;并且&#39;采取&#39;从用户输入到数据库及其关联的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

我整天都用Google搜索了,我认为正则表达式采用模式&#39; store_ &#39;并且&#39;采取_ &#39;该怎么办?还是有另一种方法来实现这一点? 谢谢你:)

3 个答案:

答案 0 :(得分:0)

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 :(得分:0)

假设您正在从PHP循环生成HTML <input ... />字段,例如:

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();
    }
}

答案 2 :(得分:0)

经过几个小时的搜索终于找到答案了 我决定改变一下输入表格。

<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 输入类型=隐藏来检索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

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

谢谢您回答并评论我的问题:)

相关问题