Ive been programming a search form with three fields, and the one that is giving me trouble is the one that use the “LIKE” clause in sql.
Here is the code:
<form method="post" action="<?php $_SERVER['PHP_SELF']?>">
<p>
<label for="nome">Nome Empresa:</label>
<input type="text" name="nome" id="nome"/>
<label for="concelho">Concelho:</label>
<select name="concelho">
<option id="" selected="selected" value="">Seleccione o Concelho</option>
<option value="1" id="1">Um</option>
<option value="2" id="1">Dois</option>
</select>
<label for="actividade">Actividade:</label>
<select name="actividade">
<option id="" selected="selected" value="">Seleccione a actividade</option>
<option value="1" id="1">Actividade Um</option>
<option value="2" id="1">Actividade Dois</option>
</select>
</p>
<p>
<input type="submit" name="pesquisar" value="Pesquisar"/>
</p>
</form>
// the sql (not all)
$nome = mysql_real_escape_string($_POST['nome']);
// Pesquisa a partir da form
if (isset($_POST['pesquisar'])) {
$queryStr = 'SELECT * FROM ';
if(!empty($nome)){
$queryStr .= 'tbl_clientes WHERE nome LIKE '%'$nome'%'';
}
Why does it give me this error twice?
Warning: Division by zero in .. on line ..
Warning: Division by zero in .. on line ..
I’m not making a Division…am i?
Thanks in advance
Yes, you do. The
%signs used byLIKEare outside the string, and hence interpreted as the modulo operator. Remove the additional ‘ signs.(Here I used a mixture of single and double quotes to sove the problem. Eoin Campbell’s solution of escaping the inner single quotes is just as valid. You will often find that you will need to use (a combination of) these techniques when programming in PHP.)