0

Hello I need to select all the NULL records in my database which have the column called "cliente_pretenece"

The problem is that I can't define it inside the sql query using IS NULL, because I am using different criteria’s for select, here is the select code:

$sql = "SELECT * FROM users WHERE 1";
if (!empty($id)) {
  $sql .= " AND userID = '$id' ";
}
if (!empty($telefono)) {
  $sql .= " AND telefono LIKE '%$telefono%' ";
}
if (!empty($cliente_pretenece)) {
  $sql .= " AND cliente_pretenece = '$cliente_pretenece' ";
}

the column cliente_pretence will always have 3 different states, and the select code from whch the call is coming is:

<select>
<option  value="SDQ">State 1</option>
<option  value="SA">State 2</option>
<option  value="NULL">Nothing</option>
</select>

So any help will be very welcome. Thanks

1 Answer 1

2

You have to use IS NULL. Modify your code so that it reacts differently to the NULL value and generates an IS NULL test instead of a comparison.

Also, learn about parametrized queries before you get an SQL injection.

Sign up to request clarification or add additional context in comments.

8 Comments

this code is for local use only it is not used in internet.
That's no excuse to be lazy. Besides what if a user enters a ' character? They'll get an error.
there isn't any <input>'s only select tags so the user don't have an option to write anything. The code is really old and I need a simple modification in other cases I have to revriew the whole system and that goal doesn't fit in the time frame.
Where does $telefono come from?
<option value="yes">Telefono</option> <option value="no">No Telefono</option>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.