我正在使用SQL和PDO编写一本食谱书。 我的'食谱'表有一个'id','name','attachment_id'列; 'attachment_id'列是外键。
'附件'表格,包含colunms'id','attachment_path'。
目前,如果删除配方,其路径存储在“附件”表中的图像将保留在那里。我想要实现的是,如果我删除配方,也需要删除属于它的“附件”。我已经做了一些研究,我明白我需要创建一个触发器但是它一直显示它是错误的,我不知道它是如何工作的......或者如何处理“分隔符”。 ...
所以......我们走了:
的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;
}
我知道delete_attachment没有任何意义.....我真的无法理解这个触发器是如何工作的和/或我应该如何构建它.....它是“delete”的正常查询?
图像上传的方式是:
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';
我知道它非常混乱,而且错了,我应该检查数据是否良好,没有空白字段.....但是一开始,我更喜欢处理功能......任何人都可以告诉我是否我肯定应该使用其中一个触发器,如果是这样......我该如何开始????
谢谢