How to make a mysql query from an array coming from html?

Asked

Viewed 2,318 times

5

I know that $cores me return the form values. What is the best way to make a query in the database with the array values $cores to pick up the cars that are present in that color?

<?php
    //PHP           
    if(isset($_POST['enviar'])){

        // Conexão com o servidor do banco de dados
        $con    = mysql_connect('host', 'user', 'pass') or die (mysql_error());
        // Seleção do banco de dados
        $dbb    = mysql_select_db('database', $con) or die (mysql_error());
        // Pega o post 'cor' e dá implode. Resultado: 00001,00002,00003 ...
        // Dependendo das cores que forem selecionadas no formulário.
        $cores  = implode(",", $_POST['cor']);
        // Pegamos os valores do array e comparamos com IN que é a mesma coisa que OR
        // com a diferença de IN ser uma instrução para a mesma coluna.
        $query  = mysql_query("SELECT * FROM carros WHERE cores IN (".$cores.")");
        // contador para confirmar a quantidade de registros retornados
        $count  = mysql_num_rows($query);
        // mostra os valores do array enviados pelo formulário
        echo $cores . "<br /><br />";

        // enquanto houver resultados relacionados a busca, buscar!!!
        while($reslt = mysql_fetch_array($query)){

            echo "<pre>";
                print_r($reslt['carro']);
            echo "</pre>";

        }

    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
</head>
<body>

</body>
    <form id="enviar" method="POST">
        <?php echo "<pre>"; ?>
        <input type="checkbox" name="cor[]" value="00001">Vermelho
        <input type="checkbox" name="cor[]" value="00002">Azul
        <input type="checkbox" name="cor[]" value="00003">Amarelo
        <input type="checkbox" name="cor[]" value="00004">Verde
        <input type="checkbox" name="cor[]" value="00005">Branco<br>
        <input type="submit" name="enviar" value="Enviar">
    </form>
</html>

Support URL: http://axitech.com.br/vista/teste1array.php

  • It was not very clear friend, you want to display in the case a list with the cars that has a certain color separating by section?

  • Responding to your commenting @user11989, I want him to simply search and show all the cars with the colors selected in the form.

2 answers

6


Try it this way, buddy

if(isset($_POST)){
    $cores = implode(",", $_POST['cor']);
    $carros = mysql_query("SELECT * FROM carros WHERE cor IN(".$cores.")");
}

1

Try this:

<form id="enviar" method="POST">
<?php
if(isset($_POST)){

    $cores = $_POST['cor'];
    echo '<pre>';
    print_r($cores);

    $carros = mysql_query("SELECT * FROM carros WHERE cor LIKE '$cores'");


while ($row = mysql_fetch_array($carros))
{
echo "<input type=\"checkbox\" name=\"$row[cor]\">$row[cor]";
}
}
?>

    <input type="submit" name="enviar" value="Enviar">
</form>

Sqlfiddle

Browser other questions tagged

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