Problem when deleting data from a table! Does not meet WHERE

0

I have this problem: when I delete a course from a student I must also delete the outstanding payments of that course, those pending payments belong to a student. The problem is that I am erasing even the payments with condition of paid, and these I can not eliminate because I need them for income reports.

These are the tables from which I delete the data:

Of the inscribed table, I eliminate the inscription, and from the table payments_students I delete the rows where the payment status is equal to zero, that is, the pending payments. But it turns out that he is eliminating me until the payments with condition of paid.

If it is of importance, in the relation of the tables I have a DELETE ON CASCADE.

This is the function in which I do the DELETE:

function eliminando_curso_student()
{
$mysqli = new mysqli('localhost','root', '' , 'academia');

$id = $_GET['id'];
$query = "DELETE FROM inscritos WHERE id = '$id'";
$sql = $mysqli->query($query);

if($sql>0):
$query2 = "DELETE FROM pagos_estudiantes WHERE id_inscripcion = '$id' AND 
estado = 0";
$sql2 = $mysqli->query($query2);
echo '<script type="text/javascript">javascript:history.back()</script>';
else:
echo mysqli_error($mysqli);
endif;
}

I deleted the registration of the course, but when it is going to delete the pending payments, it deletes even those that are in paid condition. Help please

    
asked by Alejo Mendoza 29.08.2017 в 16:33
source

2 answers

0

You have a syntax error in query2 :

$query2 = "DELETE FROM pagos_estudiantes WHERE id_inscripcion = '$id' AND 
estado == 0";

You have to put only 1 '=' instead of 2:

$query2 = "DELETE FROM pagos_estudiantes WHERE id_inscripcion = '$id' AND 
    estado = 0";
    
answered by 29.08.2017 в 16:41
0

The correct syntax is with a single "="

DELETE FROM pagos_estudiantes WHERE id_inscripcion = '$id' AND estado = 0;

However, do not forget to escape your data to avoid sql injection.

    
answered by 29.08.2017 в 16:45