Skip to content
Advertisement

Handle empty variable in a prepared statement

Sometimes my script receives a $_POST["is_searchfield"] request from a form. Then I want to use this variable as a part of a “Where” clause of a MySQL query. Otherwise this part of the “Where” clause shouldn’t afflict the rest of the query.

For the query I use a prepared statement.

If the $_POST["is_searchfield"] has a value the query works as expected, but if no variable is sent the whole query doesn’t give me any results.

How can I achieve that an empty variable doesn’t destroy the whole query and only this certain part of the “where” clause will be ignored. Also the prepared statement should continue to work.

I tried to check if the variable is set with !empty but it doesn’t change this behavoiur.

JavaScript

Advertisement

Answer

The idea is to change the query to match the data provided

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement