My objective is to try that if an item of my first table has the foreign key that would be a GENRE, it does not let me delete that GENRE in my second table, for this I do a SQL query but it keeps throwing me the error that I cannot delete my foreign key, does anyone have an idea that I could be wrong? Here I am going to leave my SQL query and my delete function to eliminate it. If the gender is not being used in the other table it lets me delete it normally...
Clarification: I have the tables linked by Genero_ID
Esta funcion es un contador en la base de datos a ver si algún genero esta siendo utilizado.
function contadorCategoria($Genero_ID){
$sentencia = $this->db->prepare("SELECT count(*) FROM generos c JOIN juegos l ON c.Genero_ID = l.Genero_ID LIKE ? GROUP BY c.Genero");
$sentencia->execute(array($Genero_ID));
$contador = $sentencia->fetchAll(PDO::FETCH_OBJ);
return $contador;
}
Funcion que hace el delete segun si el contador esta null o no...
function deleteGEN($Genero_ID){
$this->userHelper->checkLoggedIn();
$contador = $this->modelGenero->contadorCategoria($Genero_ID);
if ($contador == NULL){
$this->modelGenero->DeleteGeneroByIDGen($Genero_ID);
header("Location: " . BASE_URL);}
else {header("Location: " . BASE_URL);}}
My boards are called games and genres.
I understand that what you need is something like this, more or less as I said in the comments:
Try to see if it gives any syntax error or works fine.
Changes made
$Genero_ID
table :and then the other function has been slightly modified to receive that result in the $counter variable, which can be 0 (which means that there are no matches in the games table) or a positive value (which indicates the number of times that uses in the games table), to then do the redirection or deletion of the record in the table
generos
: