I am working in a recipe book with SQL and PDO. My 'recipes' table has a column for 'id', 'name', 'attachment_id'; The 'attachment_id' column is the foreign key.
The 'attachments' table, has colunms 'id', 'attachment_path'.
At the moment, if I remove a recipe, the image, which its path gets stored on the "attachments" table, stays there. What I want to achieve is, if I remove a recipe, the "attachment" that belongs to it also needs to be removed. I have done some research and I understand I need to create a trigger but it keeps displaying it is wrong and I can't undertand how it works....or what to do with "delimiter"....
So.... here we go:
functions.php:
 function delete_recipe($recipe_id = ':recipe_id', $attachment_id = ':attachment_id') {
              include 'db_connection.php';
        try {
            $sql = "DELETE FROM recipes ";
            $sql .= "WHERE id =:recipe_id ";
            $sql .= "LIMIT 1";
            $results = $conn->prepare($sql);
            $results->bindParam(':recipe_id', $recipe_id, PDO::PARAM_INT);
        if($results->execute()) {
          echo '1 row has been removed';  
          // if the recipe gets deleted, then delete its attachment
          delete_attachment($attachment_id);
        }
        $conn = null;
        } catch(PDOException $e) {
            echo 'Error: ' . $e->getMessage() . '<br />';
            return false;
        }
        return true;  
    }
function delete_attachment($attachment_id = ':attachment_id') {
       include 'db_connection.php';
    try {
        $sql = 'DELIMITER $$';
        $sql = 'CREATE TRIGGER image_before_delete_recipe';
        $sql .= ' AFTER DELETE ON `recipes`;
        $sql .= ' FOR EACH ROW BEGIN';
        $sql .= ' IF NEW.deleted THEN ';  
        $sql .= "DELETE FROM attachments ";
        $sql .= "WHERE id =:attachment_id ";
        $sql .= "LIMIT 1"; 
        $sql .= 'DELIMITER $$';
        $results = $conn->prepare($sql);
        $results->bindParam(':attachment_id', $attachment_id, PDO::PARAM_INT);
    if($results->execute()) {
      echo '1 row has been removed';  
    }
    $conn = null;
    } catch(PDOException $e) {
        echo 'Error: ' . $e->getMessage() . '<br />';
        return false;
    }
    return true;
}
I know the delete_attachment does not have any sense..... I literally can't understand how this trigger works and/or how I should build it..... is it instead the normal query for "delete"?
The way the image is uploades is:
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
         $attach_id = filter_input(INPUT_POST, 'attach_id', FILTER_SANITIZE_NUMBER_INT);
       $folder="images/";
       $file = $_FILES['photo']['tmp_name'];
       $file_to_upload = $folder . basename($_FILES['photo']['name']);
       if(move_uploaded_file($file, $file_to_upload)) {
           echo "File is valid, and was successfully uploaded.\n";
       if($attach_id = add_image($file_to_upload)) {
           if(add_recipe($name, $attach_id)) {
               header('Location: index.php');
            exit;
          } else {
               $error_message = "Could not add recipe";
          } 
       } else {
               $error_message = "Could not add image";
          } 
       } else {
           echo 'Upload failure';
           print_r($_FILES);
       }  
    }
    include 'includes/header.php';?>
        <div class="col-container">
          <h1>Add a recipe</h1>
          <?php
          if (isset($error_message)) {
              echo '<p class="message">' . $error_message . '</p>';
          }
          ?>
          <form method="POST" action="recipe.php" enctype="multipart/form-data">   
            <div>
              <label for="name" class="required">Name</label>
              <input name="name" type="text" value="" />
            </div> 
            <div>  
        <input name="attach_id" type="text" value="" class="hidden" />  
            <label for="file">Filename:</label>
            <input type="file" name="photo" id="photo"><br>
            </div>
            <button class="submit" type="submit" name="submit">Submit</button>
          </form>
        </div>  
    <?php include 'includes/footer.php'; 
I know it very messy, and wrong, I should check on the data is coming good, no empty fields..... but for the start, I prefer to work on the functionality... Anyone could tell me if I should definitely do this with one of those triggers, and if so.... how do I start????
Thank you
