user3393922
user3393922

Reputation:

Populating checkboxes from database table and insert their values to another table

This is my view code

<p>
   <label>item name</label>
   <span class="field"><input type="text" name="item_name" id="item_name" class="input-small" value="<?php echo set_value('item_name'); ?>" /></span>
</p>
<?php foreach($variable as $value) { ?>
      <label><?php echo $value['tag_name']; ?></label>
      <span class="field"><input type="checkbox" value="<?php echo set_value('tag_id'); ?>" /></span>                                 
<?php } ?>

Is it the right way to populate checkbox dynamically? what should i do to pass checkboxes value to database with corresponding field

i want to insert into table like following

+----------+--------------+
| item_id  |     tag_id  | 
+-------------+-----------+
| 1        | 1            |
| 1        | 2            |
| 2        | 1            |
| 2        | 2            |
| 2        | 3            |
+----------+--------------+

How should I do it?

Upvotes: 0

Views: 500

Answers (1)

Karthik P
Karthik P

Reputation: 23

 first, the line which you added 
  "<span class="field"><input type="checkbox" value="<?php echo set_value('tag_id'); ?>" /></span>" 

should be like below

<span class="field"><input type="checkbox" name="tag_id[]" value="<?php echo $value['tag_id']; ?>" /></span>      

the checkbox should be an Array type. Note the checkbox name, instead of tag_id, it should be tag_id[].

In post variable, you can capture this array value and store it into the database.

Upvotes: 1

Related Questions