My problem is the following, I am trying to show a table with the people who must cancel in the month of January or any month, I do it in the following way:
First I pass in a link the initial data via GET
<a href="calendario.php?dato=<?php echo date('01-m-Y');; ?>">
I receive that data here and execute my query:
<?php
if(isset($_GET['dato'])){
$dato2 = date('t-m-Y');
$fecha = $_GET['dato'];
$query = "SELECT * FROM students INNER JOIN inscritos ON
inscritos.id_student = students.id_students
INNER JOIN pagos_estudiantes ON pagos_estudiantes.id_inscripcion =
inscritos.id WHERE pagos_estudiantes.fecha_a_pagar BETWEEN '$fecha' AND
'$dato2'";
$sql = $mysqli->query($query);
while($row = $sql->fetch_assoc()){ ?>
<tbody>
<tr>
<td><?php echo $row['names'] ?></td>
<td><b><?php echo $row['monto'] ?></td>
<td><?php echo $row['fecha_a_pagar'] ?></td>
</tr>
</tbody>
<?php } ?>
<?php } ?>
The result I get from that is not what I wanted, it shows me all the records I have, that is, all the people who are registered in the system. And I only want to show those whose date to pay is, for example, between 01-01-2018 and 01-31-2018. Thank you in advance for your help.
Since you're using
BETWEEN
the wrong field (which is a shame and could cost you a hefty bill), the MySQL documentation says the following:That is, you will have to do a conversion of your string to a date to do the comparison. If the column were of type
DATETIME
this conversion would not be necessary.Try the query like this:
If in your column
fecha_a_pagar
you have the dates stored in the format,DD-MM-YYYY
we are facing a total nonsense, because MySQL stores the dates in theYYYY-MM-DD
. Then you'll have to reformat everything to make a proper comparison:Note: As @AlvaroMontoro already pointed out in a comment, your queries are vulnerable to SQL Injection.