Simple PHP question, I need a structure, if the course has a name, you can not delete it!

-2

Personal speech beauty then the following in delete course should be respect the idea that where can not have name linked to that course, that is, if you have name that has that course, it can not be deleted, as would mount the structure , I tried some, but it's complicated, I'm a beginner. below how this course mounted delete so far!

<?php

include "../includes/conexao.php";

   $id = $_GET['id'];


   $sql = "DELETE  FROM curso where id = $id";


   $query = mysqli_query($conexao, $sql);


if($query){
    header("location: curso.php?mensagem=sucesso");
}else{
    header("location: curso_excluir.php?mensagem=erro");
}

?>
    
asked by anonymous 17.03.2018 / 15:26

1 answer

-1

Let me get this straight. You have 2 tables: Courses Participants Courses

Do you want the course can only be deleted if there is no participant for the course in question?

If so, you have 2 ways to do it: 1 - Referential Integrity in the Database. 2 - Via PHP Code

As you say beginner, I'll give you the suggestion via code that I think is simpler to understand, but I do not think it's the right one.

You should check the attendee table and see if there are any registered participants. Ex:

$consulta_participantes = mysqli_query($conexao, "SELECT blablabla FROM participantes_cursos WHERE id_curso = '$idcurso'");

$qtd_participantes = mysqli_num_rows($consulta_participantes);

With the number of participants at hand, through the variable $ qtd_participants, make an IF to see where it will be routed.

Ex:

if($qtd_participantes == "0"){
    header("location: exclui_curso.php?id=$idcurso");
} else {
    header("location: lista_cursos.php?mensagem=Não foi possível excluir o curso pois já há participantes inscritos");
};

It was a very simplistic example but it will help you understand and solve your problem.

    
17.03.2018 / 16:07