Create PHP form using recovered result of an SQL query

Asked

Viewed 155 times

-1

I need to send what was entered in the input field and send by the POST method. My code is 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?

  • You are speaking the input that is inside the while, to add the quantity?

  • sorry, <input name='Qtd'>

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 out. But remember that this way. To put the form after tr will be breaking semantically html. For this look at this post: https://stackoverflow.com/questions/4035966/create-a-html-table-where-each-tr-is-a-form

  • Thanks, I’ll check here.

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 valuables you just do:

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

foreach( $arrayQTD as $id => $valorDoInputDesseID ) 
{
    //fazer qualquer coisa
]
  • Thank you I’ll be checking thank you

  • The first part of the code returned to me as <form action="#" method="post"></form>. Is that correct? closed form in sequence.

Browser other questions tagged

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