How to create a real-time list with PHP and Ajax

0

I'm creating a recipe site, where the ingredients will be individually registered in a table, referring to the recipe id, example of how the tables are:

ptp_receitas
id|nome
1 |receita de feijão

ptp_receitas_ingredientes
id|id_receita|ingrediente
1 |    1     | feijão
2 |    1     | bacon
3 |    1     | cebola

I created a form that inserts the values into the database, but I do not know how to insert each ingredient into a new row.

I thought of something with ajax, putting an "ingredient" input that adds the values in a multiple select, and after registering the recipe, it takes every value from that list and inserts a line in ptp_receitas_ingredientes, something like this:

<input id="ingrediente">
<button id="add_ingrediente" type="button">Adicionar Ingrediente</button>
<select id="ingredientes" multiple>
</select>
<input id="nome_receita">
<button id="cadastrar_receita" type="submit">Cadastrar Receita</button>

Is the line of reasoning for my need the same? Or is there another way to do that?

Thank you!

    
asked by anonymous 06.04.2018 / 15:01

1 answer

0

The line of reasoning is simple. Your bank structure has two tables, one of which has a foreign key (FK) referencing the other in a 1- relationship.

To insert these records, you will have to follow the order of INSERT's

  • INSERT in table "ptp_receitas"
  • Retrieve the ID you just entered (there are several ways you can do this, it depends on what technologies you are using)
  • Makes the INSERT's in the "ptp_receitas_ingredientes" table using the data received from the form and the previously retrieved ID.
  • In these steps, each INSERT will actually have to be run individually.

    You can send all data to the backend and there organize the INSERT statements that will be executed or can make an AJAX request for each serial mode statement, as long as it respects the above order.

        
    06.04.2018 / 15:56