Select date before searching

Asked

Viewed 154 times

0

I’ve tried to do a lot of things but none of them worked out, okay.

I need to make a date filter to facilitate searches, even setting a specific date ends up that all results are shown, I want to pick the date of a timestamp and I’m using datepicker to set the values, I’m probably not doing it the right way.

Just follow my code:

<?php

if(isset($_GET['submit'])){

// conexao
$db_host="localhost";
$db_username="";
$db_password="";
$db_name="";
$tabela="usuario";
$BDcoluna1="log";
$BDcoluna2="nome";
$BDcoluna3="usuario";
$BDcoluna4="email";

      mysql_connect("$db_host","$db_username","$db_password");
      mysql_select_db("$db_name");

      $query=mysql_real_escape_string($_GET['query']);

       $query_for_result=mysql_query("
            SELECT * FROM $tabela WHERE
            $BDcoluna1 BETWEEN '".$de."%' AND '".$para."%'
            AND
            $BDcoluna2 LIKE '".$query."%'
            OR
            $BDcoluna3 LIKE '".$query."%'
            OR
            $BDcoluna4 LIKE '".$query."%'
            ");
echo "<center>
<table border='1' cellpadding='5' cellspacing=0 style=border-collapse: collapse bordercolor='#4B5AAB'>";
echo "<tr> <th>Data</th>
                <th>Analista</th>
                    <th>Solicitante</th>
                <th>Centro de Custo</th>
           <th>motivos</th>
      </tr>";
echo "<h2>Resultado da Busca</h2>";

while($data_fetch=mysql_fetch_array($query_for_result))
{   echo "<tr>";
echo '<td>' .substr($data_fetch[$BDcoluna1]. '</td>', 0,160);
echo '<td>' .substr($data_fetch[$BDcoluna2]. '</td>', 0,160);
echo '<td>' .substr($data_fetch[$BDcoluna3]. '</td>', 0,160);
echo '<td>' .substr($data_fetch[$BDcoluna4]. '</td>', 0,160);
echo "</tr>";
}
mysql_close();
}
?>
  • $Would your column contain the dates? Try printing the generated query before running it to see if it was correctly generated.

  • Yes, $Bdcoluna1 is my column with the dates, and yes she printed normally. ;/

  • Why are you using '%' in the 'date' column? Is this character for LIKE not for string queries? Please put an example of the generated sql.

  • The search for date is complementary, at least it should be, it should sort the results by date to facilitate the searches, the question of '%' really think it’s not right, the LIKE are for other fields.

  • To filter by date try using "$Bdcoluna1 BETWEEN $de AND $para" recalling that your $from/$parameter must be in DB format to work, e.g. '2014-11-11' "ORDER BY $Bdcoluna1"

  • It didn’t work out, but I appreciate the help ;)

Show 1 more comment

1 answer

1

The problem is in your query.

$query_for_result=mysql_query("
        SELECT * FROM $tabela WHERE
        $BDcoluna1 BETWEEN '".$de."%' AND '".$para."%'
        AND
        $BDcoluna2 LIKE '".$query."%'
        OR
        $BDcoluna3 LIKE '".$query."%'
        OR
        $BDcoluna4 LIKE '".$query."%'
        ");

Try to insert your condition OR between parentheses:

$query_for_result=mysql_query("
    SELECT * FROM {$tabela} WHERE
    $BDcoluna1 BETWEEN '{$de}' AND '{$para}'
    AND (
        $BDcoluna2 LIKE '{$query}%' OR 
        $BDcoluna3 LIKE '{$query}%' OR 
        $BDcoluna4 LIKE '{$query}%' 
    )
");

Browser other questions tagged

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