How to query 2 columns in a single input text

Asked

Viewed 50 times

0

Hello, How do I use a single input text to query 2 columns? Currently my code has an input=select and an input=text but in the text I want to see 2 columns.. how do I do this in the sql query? Note that in my input=text in the placeholder I want to search by the "OK" number or "Description" of the item.. Someone gives me a light?

$query = ("SELECT bem, des_bem, revenda FROM AFX_BEM WHERE revenda LIKE :rev AND des_bem LIKE :bd  "); 
$stmt = $pdo->prepare($query);
$stmt->bindValue(':rev', '%' . $var1 . '%', PDO::PARAM_STR);
$stmt->bindValue(':bd', '%' . $var2 . '%', PDO::PARAM_STR);
$stmt->execute();

html

<form class="form-inline" role="form" action="lista.php" method="get">
         <div class="form-group">
             <label for="revenda">Pesquisar</label>      

             <select class="form-control" name="revenda" style="width: 140px" onchange="this.form.submit();" >
                       <option value="<?php echo $_GET["revenda"] 

                       ?>" selected><?php if(isset($_GET["revenda"]) ) { 
                       if ( ($_GET["revenda"]) == 1 ) { echo "Piracicaba"; }
                       if ( ($_GET["revenda"]) == 2 ) { echo "Botucatu"; }
                       if ( ($_GET["revenda"]) == 3 ) { echo "São Manuel"; }
                       if ( ($_GET["revenda"]) == 4 ) { echo "Lençóis Paulista"; }
                       if ( ($_GET["revenda"]) == 5 ) { echo "Jaú"; }
                       if ( ($_GET["revenda"]) == 6 ) { echo "Ibitinga"; }

                       } else {  ?>Revenda <?php } ?></option>
                        <?php
                            $linha2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
                            foreach($linha2 as $listar2){

                        ?>
 <option value="<?php echo $listar2["REVENDA"]; ?>">
    <?php echo utf8_encode($listar2["RAZAO_SOCIAL"]);    ?>
 </option>
                        <?php
                            }       
                        ?>   

                        </select>

<input class="form-control" type="text" name="bemdesc" id="bemdesc" placeholder="BEM ou Descrição" <?php if(!isset($_GET["revenda"]) ) { ?> hidden  <?php } ?> >
<button class="btn btn-default" type="submit" <?php if(!isset($_GET["revenda"]) ) { ?> hidden  <?php } ?>><i class="fa fa-search"></i></button>

2 answers

2


Do it as follows, inserting another clause in the condition of your query:

$query = ("SELECT bem, des_bem, revenda FROM AFX_BEM WHERE revenda LIKE :rev AND ( des_bem LIKE :dbd OR bem LIKE :bd ) "); 
$stmt = $pdo->prepare($query);
$stmt->bindValue(':rev', '%' . $var1 . '%', PDO::PARAM_STR);
$stmt->bindValue(':dbd', '%' . $var2 . '%', PDO::PARAM_STR);
$stmt->bindValue(':bd', '%' . $var2 . '%', PDO::PARAM_STR);
$stmt->execute();
  • I had tried to do this.. but then my query returns nothing

  • I understood your question, adjusted the query, try again now.

  • not even return. but I did the same query in the database placing the parameters manually and returned normal values.. I believe the problem is now in the treatment of the parameters in PDO.. I may be using the same bindvalue 2 times?

  • I got it this way. I don’t know if it’s the right one but it worked:

  • Maybe a solution is to put 3 parameters, give a look at the new query.

0

$var1 = $_GET["revenda"];
$var2 = $_GET["bemdesc"];
$var3 = $_GET["bemdesc"];

$query = ("SELECT bem, des_bem, revenda FROM AFX_BEM WHERE revenda LIKE :rev AND ( des_bem LIKE :bd OR bem LIKE :bd2 ) "); 
$stmt = $pdo->prepare($query);
$stmt->bindValue(':rev', '%' . $var1 . '%', PDO::PARAM_STR);
$stmt->bindValue(':bd', '%' . $var2 . '%', PDO::PARAM_STR);
$stmt->bindValue(':bd2',   $var3  , PDO::PARAM_STR);
$stmt->execute();

Browser other questions tagged

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