How to insert record into database using checkbox of another option in php?

I have a form like this:

enter image description here

I am really confused how to do all this in the database. I have a code like this:

echo "<form action='insert.php'>";
  $data = array("rice","fish","pizza","other");
  for($i=1; $i<5; $i++){
     echo "<input type='text' name='food[]' value='$i' class='food'><label>$data[$i]</label>";
  }
  echo "<input type='submit' value='ok'>";
echo "</form>";
<script>
    $(".food").change(function () {
        if (this.checked && this.value=='4') {
            $(this).next("label").after("<p id='other-text'><input placeholder='please enter food' type='text' name='otherfood[]' /></p>")
        } else {
            $("#other-text").remove();
        }
    });
</script>

Database:

order
id   id_food  other
----------------------
1       4      soup

Can you help me how to do all this in database tactics?

+4
source share
1 answer
order
id   id_food  other
----------------------
1       4      soup

I do not think it is a good idea to store data. If you really need to save id_food = 4(which applies 'other') along with 'soup', I suggest setting up the product search table as follows:

food
id   name  is_other
----------------------
1    pizza 0
5    soup  1

And changing the order table as follows

order
id   id_food  other_id
----------------------
1       4      5

, , , other textbox food, id_food food_id order .

, data

select id,name from food where is_other = 0

soup food, checkbox.

, , , , .

0

All Articles