无法将多个复选框值存储到数据库中。不确定这是否是最有效的方法,但这是我唯一能想到的。如果有更好的方法,请分享。我仍然处于测试模式,所以还没有验证。
来自数据库的项目
$get_products = $db->prepare("select * from item where user_id = '$id' ORDER BY add_date");
$get_products->execute();
while ($row = $get_products->fetch())
{
$item_id = $row['item_id'];
$user_id = $row['user_id'];
$item_name = $row['item_name'];
$products .= "<br/><input type='checkbox' name='items[]' value='$item_id' />$item_name";
}
形式
<form method="post" action="confirm.php">
<?php echo $products; ?>
<input type="submit" value="Add Items" id="add_items"/>
<input name="from_id" type="hidden" value="1">
<input name="to_id" type="hidden" value="2">
<input type="submit" name="submit" value="Submit" >
</form>
过程
if(isset($_POST['submit']))
{
$from = $_POST['from_id'];
$to = $_POST['to_id'];
$items = $_POST['items'];
if(empty($items))
{
$message = "no items in items";
exit;
}
foreach($items as $i)
{
$items .= $i. "|";
}
json_encode($items);
$sql = $db->prepare("INSERT into trans(from_id, to_id,items)VALUES(:from_id,
:to_id, :items");
$sql->bindValue('from_id', $from);
$sql->bindValue('to_id', $to);
$sql->bindValue('items', $items);
if($sql->execute())
{
header("Location: profile.php?user=1");
exit();
}