0

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();
    }

1 Answer 1

2

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();
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I commented that line out and still get nothing. when I save the items to the db do I save them with type varchar?
Yes you would. Also, have you tried outputting the value of $items before you save it into the DB?
@ShivanshuSrivastava updated code. Im getting an array to string conversion error when I try to encode the items array. I am getting all the values though
Its obvio as $items is not an array as you have used in foreach, however you can use 'while' for looping desired content along with $items though.
I have updated my answer with the code you would need in your confirm.php file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.