Register multiple elements with multiple images associated in a single BD in php

Asked

Viewed 370 times

0

I am doing a function to register products on an E-commerce platform.

The function is as follows: the user registers the base product after registering its variations (color and size, for example). If the base product has no variations, it will still be added to the table of variations, for convenience.

To register the product variation, the user selects 1 or two variations, then places the price and quantity of each of them and then selects the images for each primary variation.

Ex: Registration of a sneakers of the color red, sizes 38, 39 and 40. The user selects an X number of images related to this shoe, which has primary variation "red color", that is, these images will be displayed for all sizes of this shoe, what matters in the selection of images is the primary variation.

My problem starts here. How to connect the images to each corresponding product, where the number of images is variable? To add the other information (price and quantity) I created an array of inputs, but I do not think that with the images can be done so, due to this not limitation in the number that can be selected.

And all these products should be inserted at once into the BD, along with their images.

Part of the current code (products are created dynamically in php):

Div with product information inputs:

echo "                  <div class='small-9 columns'>";
echo "                    <input type='hidden' name='opVar1ProdutoPV[]' value='$opcao1'>";
echo "                    <input type='hidden' name='opVar2ProdutoPV[]' value='0'>";
echo "                    <div class='row'>";
echo "                      <div style='padding-top:1%' class='small-4 columns'>";
echo "                        <input name='precoReal[]' type='text' id='preco' placeholder='Preco:'>";
echo "                      </div>";
echo "                      <div style='padding-top:1%' class='small-4 columns'>";
echo "                        <input name='quantidade[]' type='text' id='qtd' placeholder='Quantidade'>";
echo "                      </div>";
echo "                      <div style='padding-top:1%' class='small-4 columns'>";
echo "                        <a id='remover$opcao1' class='button tiny small remover'>Excluir</a>";
echo "                      </div>";
echo "                    </div>";
echo "                  </div>";

Div of image registration:

echo "              <div style='float:left' class='small-12 columns'>";
echo "                <ul id='adicionarImagensPV$opcao1'>";
echo "                   <li style='width:20%;display:inline-block'>";
echo "                      <input id='imagemPV$opcao1' type='file' name='imagensPV[]' class='imagensPV'>";
echo "                      <input id='imagemPV$opcao1' type='hidden' name='imagensPV[]' class='imagensPV'>";
echo "                   </li>";
echo "                </ul>";
echo "              </div>";

Simplified versions of tables:

Product

CREATE TABLE IF NOT EXISTS produtovariacao ( idProdutoVariacao int(11) NOT NULL, idProdutoBase int(11) NOT NULL, idOpcaoVariacao1 int(11) DEFAULT NULL, idOpcaoVariacao2 int(11) DEFAULT NULL, quantidade int(11) DEFAULT NULL, precoReal varchar(45) NOT NULL, )

CREATE TABLE IF NOT EXISTS produtoimagem ( idProdutoImagem int(11) NOT NULL, idProdutoVariacao int(11) DEFAULT NULL, linkProdutoImagem varchar(150) NOT NULL, )

The FK have all been declared.

  • Pose is code you already have. You will need to use another table N - M. Example: table->imgs_produtos, columns -> id,idproduto,imagem, so just associate idproduto with the product id in your product table. When doing INSERT of a new image will just put a record in the table imgs_produtos with product id and image path.

  • I’m already using two tables, I’m having difficulty in associating the images in the corresponding products to save in the table.

  • Post the structure of your tables.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.