Having trouble storing multiple checkbox values into DB. Not sure if this is the most efficient way but it's the only thing I could come up with. If there are better ways to do it please share. I am still in test mode so there is no validation yet.
ITEMS COMING FROM DB
$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
<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>
PROCESS
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();
}