Place form data in Array

Asked

Viewed 706 times

-1

Following guys, I have a database with some items and display the items of this database using this model:

<?php $conexao = mysqli_connect("Host","User","Pass","banco");
        if( !$conexao ){
            echo "Erooooou.";
            exit;
        }

        $sql = "SELECT * FROM produtos ORDER BY id DESC";
        $consulta = mysqli_query($conexao, $sql);
        if( !$consulta ){
            echo "Erro ao realizar consulta. Tente outra vez.";
            exit;
        }


        while( $dados = mysqli_fetch_assoc($consulta) ){

            $imagem  = $dados['imagem'];

            echo "<div class='shop1'>";
            echo "<img src='$imagem'>";
            echo "<div><p>" .$dados['nome']. "</p></div>";
            echo "<div><p>" .$dados['valor']. "</p></div>";
            echo "<input type='number' name'quantidade'>";
            echo "</div>";      }?> 

This code is taking all the data from my bank and displaying, each one in a cute div, until then everything beauty, business is that I wanted to take the values typed in that input and send to a next page, so that I can manipulate itlos, and save again in another database, example:

Take all the data typed in the INPUT of all the displayed items, along with the user ID that is in $_SESSION and store them in another Logs database, for when in the user’s Logs page display the amount of each item that he requested.

Is that possible in PHP? How would you do that? And excuse me, guys, I’m still learning, bear with me!

  • You can use the $_SESSION or go through the URL with GET seusite.com?a=teste&b=teste2...

2 answers

0

I got the answer, after analyzing the answer of $Programadorcaio, I realized that it could be ugly using Array, and after studying a little I discovered that to send the input data was simply put them with same NAME but as if it were a vector, example:

echo "<input type='number' name='quantidade[]'>";

Thus, when the form is sent on the other page it is received as Array, after that I decided to write the data in a database, so it was easy to manipulate it with ease. Thank you to those who responded!

0

You can put them in a $_SESSION, example:

$_SESSION['nome'] = $dados['nome'];
$_SESSION['nome'] = $dados['valor'];

and rescues them on the other page, then at the end of the page clean up the Session...

unset($_SESSION[...]);

you can also put an input type = hidden and set value with <?php echo $dados['nome']; ?> and make a $_POST on the next page to get the content.

I hope I’ve helped in some way, something.

  • It’s not that simple, it would be if it was just an item, but the amount of items on the page will be relative to the amount of items saved in the database.

  • You can create an array in $_SESSION...

Browser other questions tagged

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