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

Asked

Viewed 56 times

-2

Speaks personal beauty then the following , excluding course should be respect the idea that , where they can not have name linked to that course, ie, if you have name that has that course, can not be excluded, am a beginner. below as this mounted course exclude 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");
}

?>
  • Avoid image, edit your question and paste your code into it

  • "where they cannot have name linked with that course" ... I didn’t quite understand. The name you refer to is a field of the course table?

  • That, it is as if when inserting the course of the '''table course'' in user, the same cannot be excluded in the table course , because the same is registered a user .

  • @Manchadomanoel put the course user table

1 answer

-1

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

You want the course to be excluded only if there is no participant for the course in question?

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

As you say beginner, I will give you the suggestion via code that I think of simplest understanding, however, I do not think it is the most appropriate.

You should make a query in the participants table and see if there are registered participants or not. 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, using the $qtd_attendees variable, do an IF to see where it will be forwarded.

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.

  • That’s right, but I did what I said, and I’m making a mistake, is that you’re not pointing the tables correctly.

  • Show the complete code and table structure, then it is easier to check.

  • ok! I’ll leave it on top,

Browser other questions tagged

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