How do I save an array of quantities and sizes to the bank for a product registration? PHP [closed]

1

I have a crud where I register the product, inside this register I have a modal where it is possible to inform the sizes and quantities of the product that I am registering, then this modal returns me an array of sizes and an array of quantities in that case, could do a foreach to insert into the database .. so far so good, but I want to store the sizes and quantities in my STOCK table where I have the following fields

id | item | quantidade | tamanho

How do I know the code of the item that I'm going to write to this table, since I'll be registering the item in question, even if I first execute the query that records in the product table, I would have some way of searching for the id of the last item inserted in the bank or something along those lines?

    
asked by anonymous 08.06.2017 / 17:53

1 answer

0

Well, I'm going to try to organize things better, because your explanation was not very clear to me at least.

Let's say we have a products table first:

CREATE TABLE IF NOT EXISTS 'produtos' (
  'ID_PRODUTO' int(11) NOT NULL AUTO_INCREMENT,
  'DESCRICAO' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY ('ID_PRODUTO')
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

This product has various sizes and sizes. There are some approaches around this, I could have for example a TEXT field in this table of products to store this array in JSON for example, but there is no Array type field in MySQL, this is usually found in NoSQL database, as is the case with MongoDB.

Knowing this we will need to create a new table with the connection of this product with its quantity and sizes. Soon we will have a table products_called_quantities :

CREATE TABLE IF NOT EXISTS 'produtos_tamanhos_quantidades' (
  'ID_PRODUTO_TAMANHO_QUANTIDADE' int(11) NOT NULL AUTO_INCREMENT,
  'ID_PRODUTO' int(11) NOT NULL,
  'TAMANHO' varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  'QUANTIDADE' int(11) NOT NULL,
  PRIMARY KEY ('ID_PRODUTO_TAMANHO_QUANTIDADE')
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

It's worth remembering that I'm not creating any Foreign Key here, but could easily be added by linking the size and quantity table to the product table.

In this way, when you save the product in the product table, you will then go through your array of quantities and sizes by inserting a line for each size and quantity in the table " products_standards_quantities ". A basic example in PHP would look like this:

<?php
    $conecta = mysql_connect("HOST", "LOGIN", "SENHA") or print (mysql_error()); 
    mysql_select_db("BANCO", $conecta) or print(mysql_error());

    $sql = "INSERT INTO produtos (DESCRICAO) VALUES ('Caixa de Papelão')"; 
    mysql_query($sql, $conecta);

    $idProdutoInserido = mysql_insert_id();

    $tamanhosQuantidade = array('15cm' => 30, '30cm' => 50, '45cm' => 20);

    foreach($tamanhosQuantidade as $key => $value){
        $sql = "INSERT INTO produtos_tamanhos_quantidades (ID_PRODUTO, TAMANHO, QUANTIDADE) VALUES (" . $idProdutoInserido . ", '" . $key . "', $value)"; 
        mysql_query($sql, $conecta);
    }
?>

In the above code we connected to the database, we performed the insertion of a product into the table, and then we got the ID that MySQL generated for that product. Then I created an example array with sizes and quantities, ran through the foreach command, performing an insert for each size.

    
08.06.2017 / 18:29