I am trying to make a query according to the checkboxes that have been selected but I can not think of the best way to do it, I would like them to help me to build the code.
Here's an example of how I'm doing it
and if I select the last three it would be like this ..
As you can see, if I select the concatenation from the second onwards, it starts with OR and I can not think of how to do it so that if I choose only two, the latter will be generated without starting with the OR.
This is the code ..
<?php
error_reporting(error_reporting() & ~E_NOTICE);
$Negado = $_POST['Negado'];
$Aceptado = $_POST['Aceptado'];
$Rapido = $_POST['Rapido'];
$Desiste = $_POST['Desiste'];
$Consulta = "SELECT * FROM filtros WHERE ";
if ($Negado != "") {
$Consulta.= " estado LIKE 'Negado' ";
}
if ($Aceptado != "") {
$Consulta.= " OR estado LIKE 'Aceptado' ";
}
if ($Rapido != "") {
$Consulta.= " OR estado LIKE 'Rapido'";
}
if ($Desiste != "") {
$Consulta.= " OR estado LIKE 'Desiste'";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Checks</title>
</head>
<body>
<form method="Post">
<input type="checkbox" name="Negado" value="Negado">Negado
<input type="checkbox" name="Aceptado" value="Aceptado">Aceptado
<input type="checkbox" name="Rapido" value="Rapido">Rapido
<input type="checkbox" name="Desiste" value="Desiste">Desiste
<input type="submit" name="enviar" value="Enviar">
</form>
<br>
<?php echo "$Consulta"; ?>
</body>
</html>
If there is any other way to make this query with the checks better, I am open to options.
Update ..
I have tried adding the code 1=1
to the root query, and putting OR
as follows ...
<?php
error_reporting(error_reporting() & ~E_NOTICE);
$Negado = $_POST['Negado'];
$Aceptado = $_POST['Aceptado'];
$Rapido = $_POST['Rapido'];
$Desiste = $_POST['Desiste'];
$Consulta = "SELECT * FROM filtros WHERE 1=1 ";
if ($Negado != "") {
$Consulta.= " OR estado LIKE 'Negado' ";
}
if ($Aceptado != "") {
$Consulta.= " OR estado LIKE 'Aceptado' ";
}
if ($Rapido != "") {
$Consulta.= " OR estado LIKE 'Rapido'";
}
if ($Desiste != "") {
$Consulta.= " OR estado LIKE 'Desiste'";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Checks</title>
</head>
<body>
<form method="Post">
<input type="checkbox" name="Negado" value="Negado">Negado
<input type="checkbox" name="Aceptado" value="Aceptado">Aceptado
<input type="checkbox" name="Rapido" value="Rapido">Rapido
<input type="checkbox" name="Desiste" value="Desiste">Desiste
<input type="submit" name="enviar" value="Enviar">
</form>
<br>
<?php echo "$Consulta"; ?>
</body>
</html>
But with that query it brings me all the records of the table if I select one or two checkbox ....