Create form in PHP using result retrieved from a SQL query

-1

I need to send what was typed in the input field and send via the POST method. My code looks like this:

<?php
include_once("../controle/conexao.php");
//Recuperar o valor da palavra
$usuarios = $_POST['palavra'];
echo '$usuarios';

//Pesquisar no banco de dados o nome do usuario referente a palavra digitada
$result_usuarios = "SELECT * FROM cadastro_produtos WHERE descricao LIKE '%$usuarios%' OR id LIKE '%$usuarios%' LIMIT 1";
$resultado_usuarios = mysqli_query($conn, $result_usuarios);

if(mysqli_num_rows($resultado_usuarios) <= 0 ){
    echo "Nenhum usuario encontrado";
}else{ 
    while($rows_usuarios = mysqli_fetch_assoc($resultado_usuarios)){
        echo "<tr>";
        echo "<td class='text-center'>".$rows_usuarios['id']."</td>";
        echo "<td class='text-center'>".$rows_usuarios['descricao']."</td>";
        echo "<td class='text-center'><input name='qtd'></td>";
        echo "<td class='text-center'> 
                <button type='submit' class='btn btn-xs btn-primary'>
                    Adicionar                   

            </td>";
        echo "</tr>";

    }
}
?>

Does anyone have any suggestions?

    
asked by anonymous 17.08.2016 / 22:58

2 answers

0

Then for each line (tr) you will have to put a form.

For example:

echo "<tr>";
   echo '<form action="#" method="post">';
     echo "<td class='text-center'>".$rows_usuarios['id']."</td>";
     echo "<td class='text-center'>".$rows_usuarios['descricao']."</td>";
     echo "<td class='text-center'><input name='qtd'></td>";
     echo "<td class='text-center'> 
                <button type='submit' class='btn btn-xs btn-primary'>
                    Adicionar</button></td>";
     //no php você vai precisar identificar de quem é a quantidade informada
     //para isso você poderá usar um input hidden
     echo '<input type="hidden" name="id_usuario" value="'.$rows_usuarios['id'].'">';
  echo '</form>';
echo "</tr>";

This will work. But remembering it that way. Putting the form after the tr will be breaking the html semantically. To take a look at this post: link

    
18.08.2016 / 03:51
0

You can do it in a simpler way, using the id of each product in an array of quantities:

echo '<form action="#" method="post">';
    while( $rows_usuarios = mysqli_fetch_assoc( $resultado_usuarios ) )
    {
        echo "<tr>";
        echo "<td class='text-center'>".$rows_usuarios['id']."</td>";
        echo "<td class='text-center'>".$rows_usuarios['descricao']."</td>";
        echo "<td class='text-center'>";
        echo "<input name='qtd['".$rows_usuarios['id']."']' >";
        echo "</td>";
        echo "<td class='text-center'> 
                <button type='submit' class='btn btn-xs btn-primary'>
                    Adicionar                   

            </td>";
        echo "</tr>";

    }
echo '</form>';

Then to get the values just do:

$arrayQTD = filter_input(INPUT_POST, 'qtd', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);

foreach( $arrayQTD as $id => $valorDoInputDesseID ) 
{
    //fazer qualquer coisa
]
    
18.08.2016 / 04:07