How to delete records from multiple tables at the same time?

0

I want to give delete in a student so I want to delete the records of that same student of all the tables where it is and its image of the folder where it is was guarded for this I use the following code: p>

<?php
    /* 
     * Verifica se a variável com o id do aluno está definida
     * Se não estiver volta à página inicial
     */
    if (!isset ($_GET['idc']))
        print "<meta HTTP-EQUIV='Refresh' CONTENT='0;URL=menuadmin.php?id=4'>";

    // Pega no registo selecionado para saber o nome 
    // da imagem ($al_img) para depois a eliminar
    $sql = "SELECT * FROM Aluno WHERE al_id = ".$_GET['idc'];
    $result = mysql_query($sql); 
    if (mysql_num_rows($result)!= 0){ 
        $registo = mysql_fetch_assoc($result);
        extract($registo);

        //Elimina a imagem
        unlink("imagens/aluno/".$al_img);
        }

    /* Elimina o registo */
    mysql_query("DELETE FROM Inscricao, 
                             Aluno, 
                             EncarregadoDeEducacao, 
                             Musica_Aluno, 
                             OutrasAtividades_Aluno, 
                             Explicacoes_Aluno, 
                             Psicologia_Aluno, 
                             SalaDeEstudo_Aluno  
                 WHERE 
                             Inscricao.al_id = Aluno.al_id AND 
                             EncarregadoDeEducacao.ee_id = Inscricao.ee_id AND 
                             Musica_Aluno.al_id = Inscricao.al_id AND 
                             OutrasAtividades_Aluno.al_id = Inscricao.al_id AND
                             Explicacoes_Aluno.al_id = Inscricao.al_id AND 
                             Psicologia_Aluno.al_id = Inscricao.al_id AND 
                             SalaDeEstudo_Aluno.al_id = Inscricao.al_id AND 
                             Inscricao.al_id = ".$_GET['idc'])

    echo mysql_error();
    /* Verifica se deu erro ao eliminar o registo */ 
    if (mysql_error()){
        echo "<script type='text/javascript'>";
        echo 'alert ("Erro ao eliminar ao aluno...");';
        echo "</script>";
    }

    /* Volta à página inicial */ 
    print "<meta HTTP-EQUIV='Refresh' CONTENT='0;URL=menuadmin.php?id=4'>";


?>

The error you give me is as follows:

  

Parse error: syntax error, unexpected T_ECHO in   C: \ xampp \ htdocs \ estagio \ eliminaraluno.php on line 32

What's wrong with the code?

-------- EDITED: ------

I already put ; now gives me more error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Inscricao.al_id = Aluno.al_id AND Encarregad' at line 1
    
asked by anonymous 21.04.2015 / 12:51

1 answer

1

You have to specify which tables you want to delete, you just entered the relationship tables. Try to put the tables between DELETE and FROM , like this:

mysql_query("DELETE 
                             Inscricao.*, 
                             Aluno.*, 
                             EncarregadoDeEducacao.*, 
                             Musica_Aluno.*, 
                             OutrasAtividades_Aluno.*, 
                             Explicacoes_Aluno.*, 
                             Psicologia_Aluno.*, 
                             SalaDeEstudo_Aluno.*
                FROM         Inscricao, 
                             Aluno, 
                             EncarregadoDeEducacao, 
                             Musica_Aluno, 
                             OutrasAtividades_Aluno, 
                             Explicacoes_Aluno, 
                             Psicologia_Aluno, 
                             SalaDeEstudo_Aluno  
                 WHERE 
                             Inscricao.al_id = Aluno.al_id AND 
                             EncarregadoDeEducacao.ee_id = Inscricao.ee_id AND 
                             Musica_Aluno.al_id = Inscricao.al_id AND 
                             OutrasAtividades_Aluno.al_id = Inscricao.al_id AND
                             Explicacoes_Aluno.al_id = Inscricao.al_id AND 
                             Psicologia_Aluno.al_id = Inscricao.al_id AND 
                             SalaDeEstudo_Aluno.al_id = Inscricao.al_id AND 
                             Inscricao.al_id = ".$_GET['idc'])

Another way to do this is to add foreign key to all tables with DELETE = CASCADE .

    
21.04.2015 / 17:10