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

Asked

Viewed 5,886 times

0

I want to give delete in a student for that I want to delete the records of that same student of all the tables where he is and his image of the folder where it is was saved for that I use the following code :

<?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 mistake you give me is this: :

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’ve already put the ; now give me this mistake :

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
  • Possibly it’s because of the lack of ; after the method mysql_query().

  • it is very likely that it is the missing point and comma at the end of the function mysql_query();

  • take a look at the mysql ON DELETE CASCADE command could be very useful in this situation

  • I’ve already put the ; now give me this 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 Registration.al_id = Student.al_id AND Guardian' at line 1

  • Put ON DELETE CASCADE on fK’s

  • http://stackoverflow.com/a/734610/1817673

Show 1 more comment

1 answer

1


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

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 it is to add external key to all tables with the DELETE = CASCADE.

  • thus not from the error but does not delete the records from the database.

  • That way it works, but you can put a ". *" in front of the tables. I corrected my answer that way.

  • If you do not delete any record, it is because you have some problem in the relationship between the tables. Redo SQL by swapping DELETE for SELECT and see if it returns any results.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.