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();
}
The first issue I see is that you are overwriting your $items
variable with a blank string when you do this:
$items = $_POST['items'];
$items = "";
This means you will be running a foreach
on a blank string.
Although a better way to store the values from your checkboxes would be to just json_encode()
the original $items
variable. This will encode the value you receive from your form into a JSON string which can safely be stored in a database.
Then when you want to get the data back out of the database, simply run json_decode()
on the JSON string and the JSON string will be converted back to an array.
Though, if you want an associative array to be returned from json_decode()
, be sure to pass true into the second parameter like so:
$indexed_array = json_decode($some_array);
$associative_array = json_decode($some_array, true);
EDIT
Providing the data is being passed from the form, this is what you will need in your confirm.php file:
if(isset($_POST['submit']))
{
$from = $_POST['from_id'];
$to = $_POST['to_id'];
$items = $_POST['items'];
if(empty($items))
{
$message = "no items in items";
exit;
}
$items = 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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.