Issue with filtering down MySQL database with internal search PHP

Solution:
You have a typo. This:
$POST['search_box']
Should be this:
$_POST['search_box']
Which means you're creating an empty variable ($search_term
). You'd see this if you had error reporting on. Next time when you develop, put this at the top of your scripts:
ini_set('display_errors', 1);
error_reporting(-1); // or you could use E_ALL
And last but not least, you need a space before yourWHERE
:
$sql .= " WHERE City = '{$search_term}'";
Notes
- You also need to run the query (ie,
mysql_query()
). - You'd be better off learning PDO / Mysqli Prepared Statements as
mysql_*
is deprecated.
One more thing to note with your search query. You're only going to find an exact match to the search query that was submitted. You're best to stick with best practice and search using :
$sql .= " WHERE City LIKE '%{$search_term}%'";
Answer
Solution:
Make sure your $_POST variables are named correctly, and there is a space before the WHERE clause. Try this:
$sql = "SELECT * FROM Table";
if (isset($_POST['search_box'])) {
$search_term = mysql_real_escape_string($POST['search_box']);
$sql .= " WHERE City = '{$search_term}'";
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.