I am developing a phonebook application on PHP. After typing 3 characters, the search script is fired. This query works well. But I have discovered that if I enter the search box %%%
characters the page lists all the records which I do not want it to do.
$value = "%" . $_POST['value'] . "%";
$query = $db->prepare("SELECT
k.id,
k.name,
k.surname,
k.phone,
k.email,
u.title,
g.job,
b.dept
FROM
persons k
JOIN titles u ON k.title_id = u.id
JOIN departments b ON k.dept_id = b.id
JOIN jobs g ON k.job_id = g.id
WHERE
active = 1 AND
(
LOWER(k.name) LIKE :v1 OR
LOWER(k.surname) LIKE :v2 OR
LOWER(k.phone) LIKE :v3 OR
LOWER(k.email) LIKE :v4
)
ORDER BY
dept,
name,
surname"
);
$query->bindValue(":v1", strtolower($value), PDO::PARAM_STR);
$query->bindValue(":v2", strtolower($value), PDO::PARAM_STR);
$query->bindValue(":v3", strtolower($value), PDO::PARAM_STR);
$query->bindValue(":v4", strtolower($value), PDO::PARAM_STR);
$query->execute();
How can I prevent this bug?