我有一个foreach循环,可以在表格中输出我的产品。代码如下所示:
<table class="table table-striped table-bordered table-hover" id="sample_2">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Quantity</th>
<th style="width: 9%;"></th>
</tr>
</thead>
<tbody>
<?php foreach ($inputs as $package){
?>
<form action="" method="post">
<tr>
<td><?php echo $package->id; ?></td>
<td><?php echo $package->name; ?></td>
<td><input type="text" name="quantity" placeholder="Quantity for <?php echo $package->name; ?>" class="form-control"></td>
<td style="text-align: center;"><button name="submit" type="submit" id="submit" class="btn blue"><i class="fa fa-check"></i> submit</button></td>
</tr>
</form>
<?php }
?>
</tbody>
</table>
好的我现在需要创建一些东西来将字段插入到mysql表中。但我如何得到$package->id, $package->name
,它们只是回声?我需要把它插入:
CREATE TABLE IF NOT EXISTS `shop_packages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`productid` int(11) NOT NULL,
`quantity` int(11) NOT NULL,
`worker` varchar(265) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
提交代码1。
if (isset($_POST['submit'])) {
$productid = $_POST['id'];
$productname = $_POST['name'];
$quantity = $_POST['quantity'];
$worker = $user['name'];
$workerid = $user['id'];
$result = $user->add_packages($productid,$productname,$quantity,$worker,$workerid);
}
的print_r($ _ POST [ '包'])
Array ( [0] => Array ( [id] => 1 ) [1] => Array ( [name] => Maxizoo Spåner 5 kg ) [2] => Array ( [quantity] => ) [3] => Array ( [id] => 4 ) [4] => Array ( [name] => Minihemp 4 kg. ) [5] => Array ( [quantity] => ) [6] => Array ( [id] => 5 ) [7] => Array ( [name] => Maxizoo Hø 2 kg. ) [8] => Array ( [quantity] => 2 ) [9] => Array ( [id] => 6 ) [10] => Array ( [name] => Maxizoo Halm 2 kg ) [11] => Array ( [quantity] => 1 ) )
更新:
if (isset($_POST['submit'])) {
foreach ($_POST['package'] as $p){
$productid = $p['id'];
$productname = $p['name'];
$quantity = $p['quantity'];
}
$worker = $user['name'];
$workerid = $user['id'];
$result = $user->add_packages($productid,$productname,$quantity,$worker,$workerid);
}
答案 0 :(得分:0)
您需要使用表单发送它们,例如使用隐藏的输入。
<form action="" method="post">
<table>
<tr>
<td><?php echo $package->id; ?></td>
<td><?php echo $package->name; ?></td>
<td><input type="text" name="quantity" placeholder="Quantity for <?php echo $package->name; ?>" class="form-control"></td>
<td style="text-align: center;">
<input type="hidden" name="package_name" value="<?php echo $package->name; ?>">
<input type="hidden" name="package_id" value="<?php echo $package->id; ?>">
^^
<button name="submit" type="submit" id="submit" class="btn blue"><i class="fa fa-check"></i> submit</button>
</td>
</tr>
</table>
</form>
然而,你使用TR
和TD
,它们应该只在一个表中,所以我也添加了一个表。