出于学习目的,我正在oop php中针对属性/广告开发cms。我正在尝试使用ajax和php从数据库中删除照片。我在视图中单击照片上的X按钮时遇到麻烦,将其删除,但是当我刷新页面时,它再次弹出,并且也没有将其从数据库中删除。任何帮助是极大的赞赏。这是我的代码:
index.php:
<?php foreach ($data1 as $key => $value) : ?>
<?php if($key == 1) { ?>
<div class="img-wrap">
<span class="close">×</span>
<img src="<?php echo '/public/photos/'.$value->name.'.'.$value->extension ?>" data-id="<?php echo $value->name ?>" width="150" height="150">
</div>
<?php } else { ?>
<div class="img-wrap">
<span class="close">×</span>
<img src="<?php echo '/public/photos/'.$value->name.'.'.$value->extension ?>" data-id="<?php echo $value->name ?>" width="150" height="150">
</div>
<?php } ?>
<?php endforeach; ?>
<script>
$('.img-wrap .close').on('click', function() {
var that = this;
var id = $(that).closest('.img-wrap').find('img').data('id');
var confirmation = confirm("Are you sure you want to delete this picture?");
if (confirmation) {
$.ajax({
url: 'ads/deletephoto',
//dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify( { "name": id } ),
processData: false,
success: function( data, textStatus, jQxhr ) {
console.log( $(that).closest('.img-wrap') );
$(that).closest('.img-wrap').hide();
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
}
});
</script>
AdModel:
public function deletePropertyPhoto($id)
{
$this->db->query('DELETE FROM photos WHERE id=(SELECT photo_id FROM property_photo WHERE property_id=:property_id)');
$this->db->bind(':property_id', $id);
$this->db->execute();
$this->db->query('DELETE FROM property_photo WHERE property_id=:property_id');
$this->db->bind(':property_id', $id);
$this->db->execute();
}
AdsController:
public function deletePhotoAction()
{
$userinfo = $this->Auth->Auth(['admin', 'moderator']);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$this->Auth->isSet($_GET['id'], "ads/index");
$photo = $this->AdModel->deletePropertyPhoto($_GET['id']);
if ($photo != false) {
if (file_exists('public/photos/' . $photo->photo)) {
unlink('public/photos/' . $photo->photo);
}
}
redirect('ads/index');
echo "Property is not found!!!";
}
}