[英]Saving data with dynamic input name to database?
好吧,在此之前,我是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
我整天都在被Google搜索,我认为带有模式'store_ '和'take_ '的正则表达式应该这样做吗? 还是有另一种方法可以做到这一点?
谢谢 :)
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
假设您正在通过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
)
)
store
和take
数组键是项目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();
}
}
经过几个小时的搜索,终于找到了答案
我决定对输入表单进行一些更改。
<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个输入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
我知道这不是很漂亮,但是可以完成工作。
谢谢您对我的问题的回答和评论:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.