1
I have a question. I have a php page with several select options to filter a search in the database. However, I cannot use the selected values in the query. I did as an example I saw on another site, an array to store only the values of the select options that were filled and concatenate this array in the 'Where' clause of my query... unsuccessfully !
$tipoentradalog = post('tipoentradalog');
$usuario = post('usuario');
$codcliente = post('codcliente');
$where = Array();
if ($tipoentradalog) {
$where[] = " 'tipoentradalog' = '{$tipoentradalog}'";
}
if ($usuario) {
$where[] = " 'usuario' = '{$usuario}'";
}
if ($codcliente) {
$where[] = " 'codcliente' = '{$codcliente}'";
}
$sql = "select
l.datacriacao,
tl.nome as tipo,
l.descricao,
cli.nome as cliente,
us.nome as usuario,
l.codigo
from log l
inner join cliente cli on cli.codcliente=l.codcliente
inner join usuario us on us.codusuario=l.codusuario
inner join empresa emp on emp.codempresa=l.codempresa
inner join tipoentradalog tl on tl.codtipoentradalog=l.codtipoentradalog;";
if(sizeof($where)) {
$sql .= ' WHERE ' . implode(' AND ', $where);
}
$rst = my_query($connR, $sql);
Can anyone help me? Thank you
use $_POST instead of post, I think the first problem is this.
– RFL
So, here where I work we already have a function defined for post and get methods... I think it is to avoid sql Injection rs I used this post in other parts of the code and it worked normally.
– goldenleticia
What are the other problems ? rsrs am new with web programming
– goldenleticia
try using the syntax
alias.coluna. You are using alias for tables and select but not for conditions. Some column that appears in more than one table may be generating an exception.– Richard Dias
Richard, I tried to use this alias scheme, but it didn’t happen... When I select the filters, the following error appears: 'WHERE 'tl.nome' = '46' AND 'cli.nome' = '172808''... I think he’s taking the primary keys of the records I need instead of the field I want to show... how do I change this ?
– goldenleticia