Deleting rows from multiples tables at once I have a question about this piece of code I just wrote. I have a problem with the syntax, the first query executes its purpose, the problem is in the second query which does not.

if(isset($_POST['deletecontr'])){
  $dcquery = $_POST['dni']; // This get the ID of the person who I want to delete.
  $catchempr = ("SELECT `empresa` FROM `contratistas` WHERE dni='$dcquery'"); //This catch the name of the company of that person
  $query = ("DELETE FROM `contratistas` WHERE dni='$dcquery'"); // This works fine
  $query2 = ("DELETE FROM `empleados` WHERE empresa='$catchempr'"); This does not.
  mysqli_query($link,$query);
  mysqli_query($link,$query2); 
  echo "La empresa a sido eliminada de la base de datos";


I hope you guys can see something that I'm not seeing.
If you change your code to this, it'll work:

if(isset($_POST['deletecontr'])){
  $dcquery = $_POST['dni']; // This get the ID of the person who I want to delete.
  $catchempr = ("SELECT `empresa` FROM `contratistas` WHERE dni='$dcquery'"); //This catch the name of the company of that person
  $query = ("DELETE FROM `contratistas` WHERE dni='$dcquery'"); // This works fine
  $query2 = ("DELETE FROM `empleados` WHERE empresa in ($catchempr)"); This does not.
  mysqli_query($link,$query);
  mysqli_query($link,$query2); 
  echo "La empresa a sido eliminada de la base de datos";


By doing this, I have changed the query from roughly:

DELETE FROM `empleados` WHERE empresa='SELECT `empresa` FROM `contratistas` WHERE dni='$dcquery'' -- // ignore the $dcquery part


to:

DELETE FROM `empleados` WHERE empresa in (SELECT `empresa` FROM `contratistas` WHERE dni='$dcquery')


By expanding the query strings you can see that the first query is not valid for a couple of reasons.

I'll also add that this code is susceptible to SQL Injection because you are not verifying or filtering the input string $_POST['deletecontr']. To mitigate this, use bind variables/prepared statements.