0

Hello I'm working in an interactive catalog that shows information from a data base, right now I get one of the query components from a variable called "$productos", when $productos is set into a $_POST array the querY workS.

I need to create an update, insert and delete modules.

So my question is how can I keep the $productos' variable value stored and use other froms to get more information in the same $_POST array? Or how can you manage multiple $_POST arrays? (SORRY, I´M TRYING TO BE AS CLEAR AS POSSIBLE)

Here I get the $productos value:

    <form action="index.php" method="post">
            <select name="productos">
                <option value="cat_carne">Selecciona un producto</option>
                <option value="cat_carne">Carne</option>
                <option value="cat_pescado_mariscos">Pescado y Mariscos</option>
                <option value="cat_aves">Aves</option>
                <option value="cat_varios">Varios</option>
                <option value="cat_abarrotes">Abarrotes</option>
                <option value="cat_congelados">Congelados</option>
                <option value="cat_fruta_verdura">Frutas y Verduras</option>
                <option value="cat_lacteos">Lacteos</option>
                <option value="cat_destilados">Destilados</option>
                <option value="cat_vinos">Vinos</option>
                <option value="cat_cerveza">Cerveza</option>
                <option value="cat_refresco">Refrescos</option>
                <option value="cat_mezclador">Mezclador</option>
                <option value="cat_cafe">Café</option>
                <option value="cat_te">Té</option>
                <option value="cat_cigarros">Cigarros</option>

            </select>
            <input type="submit" name="prodsel" value="Enviar">
            <?php $producto = $_POST['productos']; ?>

This is the query:

    $sql = "select

                    cc.id_".$producto." as ID
                  , cc.subtipo as subtipo
                  , prd.productos as productos
                  , pro.proveedor as proveedor
                  , cc.costo_unitario_nov_19    as costo_actual
                  , cc.costo_lt_kg_nov_19       as costo_por_unidad_actual
                  , cc.costo_unitario_oct_19    as costo_mes_anterior
                  , cc.costo_lt_kg_oct_19       as costo_por_unidad_anterior
                  , round((((cc.costo_unitario_nov_19 - cc.costo_unitario_oct_19) / cc.costo_unitario_nov_19)*100), 2)as Porcentaje_Aumento
    from          
                    ".$producto." cc
                    left outer join
                    productos prd on                                           cc.id_producto = prd.id_prod
                    left outer join
                    proveedores pro on
                    cc.id_proveedores = pro.id_proveedores";

Then I have this other form:

     <?php  
           $queryid = "SELECT id_".$producto." as ID FROM ".$producto." ORDER BY id_".$producto." ASC";
                $result = mysqli_query($connection, $queryid) or die ("Bad SQL: $queryid");
                ?>
                <form action="index.php" method="post">
                <select name="id">
                    <option value="">Seleccione el id a actualizar</option>
                    <?php
        if($result->num_rows > 0){ 
            while($rowid = $result->fetch_assoc()){  
                echo '<option value="'.$rowid['ID'].'">'.$rowid['ID'].'</option>'; 
            } 
        }else{ 
            echo '<option value="">Productos no disponibles</option>'; 
        } 
        ?>
                </select>
                <input type="submit" name="idsel" value="Enviar">
                <?php $upval1 = $_POST['id']; ?>
                </form>

When I hit the second input button the whole thing blows up and give me an error

Monk LikeaSir
  • 33
  • 2
  • 7
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – B. Fleming Dec 04 '19 at 23:34
  • add a hidden input with that value, like "" – Alberto Sinigaglia Dec 04 '19 at 23:34
  • @B.Fleming I don't think so I´m trying to create multiple post arrays or store data into a post array coming from different sources (inputs) I know that php os server side. I´m sorry english is not my first langauge so I may not be explaining myself correctly – Monk LikeaSir Dec 04 '19 at 23:43
  • 1
    You may know that PHP is a server side language, but your code above indicates that you don't fully understand the flow of logic between client and server yet. PHP is creating that form to send back to the web browser at the same time that it's running your query. Because of this, the query is running when `$producto` is empty (or null), which is likely causing your query to throw an error. – B. Fleming Dec 04 '19 at 23:56
  • Any HTML in your PHP code should be thought of as a string on the server. Until PHP has stopped running, the user's web browser hasn't even seen that HTML yet. – B. Fleming Dec 04 '19 at 23:58
  • @B.Fleming that makes sense, but how can I keep that $productos value stored and then add otrher information in to $_POST? – Monk LikeaSir Dec 04 '19 at 23:58
  • 1
    That depends on what exactly you're trying to accomplish and how permanent you want the storage. If you want all previous values stored permanently, then you would need to use a database to store those. If you want to store it only for the user's current session, then you could place `session_start()` at the beginning of every script and then use `$_SESSION` to store and retrieve data. If you want to store it only while the user is currently viewing the page, then you could maintain a list of "hidden" inputs in your form with each entry having the same array `name` e.g. `name="previous[]"`. – B. Fleming Dec 05 '19 at 00:07
  • 1
    You'll need to do a bit of research to figure out what will work best for you. Once you've figured that out, try again using the new approach. If you get stuck again, feel free to ask a new question regarding what your new problem is. – B. Fleming Dec 05 '19 at 00:08

0 Answers0