I'm trying to query a database using a variable to detect the chosen column along with the search term.
For simplicity I removed the DB connection error handling code. The problem code is:
$find = mysqli_real_escape_string($dbc, test_input($_POST["find"]));
$field = $_POST["field"];
$data = mysqli_query($dbc, "SELECT * FROM ticket WHERE '$field' = '$find'");
//Results
while($result = mysqli_fetch_array($data)) {
echo "Result 1" . $result['number'];
echo "<br>";
echo "<br>";
echo "Result 2" . $result['description'];
echo "<br>";
echo "<br>";
echo "Result 3" . $result['contact'];
echo "<br>";
echo "<br>";
echo "Result 4" . $result['assignee'];
echo "<br>";
echo "<br>";
echo "Result 5" . $result['priority'];
echo "<br>";
echo "<br>";
}
$anymatches = mysqli_num_rows($data);
if ($anymatches == 0) {
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
I enter a term in a textbox ($find
) and choose what Im searching for from a dropdown menu ($field
) e.g. I select assignee.
Even though I know its in the database, I still returns the "Sorry, but we can not find an entry to match your query".
However, if I change the $field variable within the MYSQL query to an actual column name. Example:
$data = mysqli_query($dbc, "SELECT * FROM ticket WHERE assignee = '$find'");
it returns with the correct data. I even echo'd the $field
variable during execution to make sure its holding the correct selection, and it does. Any ideas?
Thanks