2017-06-05 96 views
0

好吧,在此之前,我是一名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}是从数据库表 '项目' coloumn '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 

我一直在用Google搜索了一整天,我想用正则表达式模式“store_ '和'take_'应该这样做?或者有另一种方法来完成这个?
三江源:)

+1

这岂不是更容易使用的阵列,例如:'<输入值=“0”类型=“文本”名称=“店[]。 .. />' - 那么你可以'foreach($ _ POST ['store']为$ id => $ value){...}' – CD001

+0

你想举个例子吗? @ CD001 – Dika

回答

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 
+0

好吧,它 – Dika

+0

你需要在db上存储多少个输入和存储输入? –

+0

取决于用户。有多少用户有物品。如果用户有1个物品,则它会创建2个输入,一个用于存储新值的项目,另一个拿它。 – Dika

0

说你生成的HTML从PHP循环<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(); 
    } 
} 
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 

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

三江源回答和评论对我的问题:)

相关问题