Query Select Option PHP

Asked

Viewed 491 times

2

How to do a query with select option that the user can choose?

On the frontend:

<form method="post" action="Procurarquery.php"  enctype="multipart/form-data">
    <span class="LABEL4CSS" id="LABEL4">Estado</span>
    <select name="estado" size="1" width="195" class="COMBOFABCSS" id="COMBOFAB" tabindex="1">
        <option value="Indiferente">Indiferente</option>
        <option value="Autorizado">Autorizado</option>
        <option value="Condicionado">Condicionado</option>
        <option value="Não Autorizado">Não Autorizado</option>
    </select>

In the backend:

 if (isset($_POST['estado'])) {
     switch($_POST['estado']) {
             case 'Indiferente':
                 $sql = "QUERYYYYYY";
                 $qr = mysql_query($sql) or die(mysql_error());
                 break;
             case 'Autorizado':
                 $sql = "Query Autorizado";
                 $qr = mysql_query($sql) or die(mysql_error());
             break;

Now I have a doubt. If I want to put another one ($_POST['Mes']) How can I select STATUS and MONTH at the same time?

  • It depends on how you are saving the data in the database. All in a single, separate field...?

  • From what I understand you want to know how many choices each opiton value has had, no?

  • 1

    This question is unclear... do you want to query the database (Mysql for example) or use a query string (url)? or neither one or the other?

  • And want to query the database or another page?

  • I want to query the Database. To show what was requested in the Options value

  • @user3253195, What is the database? already have? is Mysql? can use jQuery or Mootools for example or prefer simple javascript?

  • Yes a Mysql Database. I want to use the simplest to display the query

Show 2 more comments

2 answers

1

It depends a lot on what is selectable or not, what is mandatory, etc.

From what I understand of the question I would do so:

    $sql = "SELECT * FROM bd_table";

    if (isset($_POST['estado'])) {
       switch($_POST['estado']) {
          case 'Indiferente':
             $sql .= " WHERE estado = 1";
             break;
          case 'Autorizado':
             $sql .= " WHERE estado = 2";
              break;
       }
    }

    if (isset($_POST['mes'])) {
        if(isset($_POST['estado'])) 
           $sql .= " AND "; 
        else
           $sql .= " WHERE ";

        $sql .= "mes = '".$_POST['mes']."'";
    }

    $qr = mysql_query($sql) or die(mysql_error());

0

//define a variável mes
$qmes = "";
//se estiver preenchida adiciona valor
if (isset($_POST['mes'])) {
    $qmes = " AND col_mes = '".$_POST['mes']."'"; 
}

if (isset($_POST['estado'])) {
     switch($_POST['estado']) {
             case 'Indiferente':
                 $sql = "SELECT * FROM bd_table WHERE col_estado = 1".$qmes;
                 break;
             case 'Autorizado':
                 $sql = "SELECT * FROM bd_table WHERE col_estado = 2".$qmes;
             break;
// tira o comando de dentro do case
                 $qr = mysql_query($sql) or die(mysql_error());

}

If I understand, this should help

Browser other questions tagged

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