下面是我从数据库中删除记录的html,ajax和数据库脚本,
HTML
<input type="file" name="gallery[]" id="gallery" data-maxupload="<?php echo $maxupload; ?>" multiple="">
<div>
<img src="..." class="item-1768"> <span class="del_button-" id="del-1768">X</span>
........
</div>
AJAX
$("body").on("click", ".del_button", function(e) {
e.preventDefault();
var clickedID = this.id.split('-');
var DbNumberID = clickedID[1];
var myData = 'recordToDelete='+ DbNumberID;
var base_url = '<?php echo base_url(); ?>';
$('.item_'+DbNumberID).addClass( "sel" );
$(this).hide();
$.ajax({
type: "POST",
url: base_url + "client/delete_gallery_image",
dataType: "text",
data:myData,
success:function(response){
$('.item_'+DbNumberID).fadeOut();
console.log(response);
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
});
模型,
public function delete_gallery_image($id){
$this->db->delete('employer_gallery', array('id' => $id));
return $this->db->affected_rows();
}
此代码的基本思想是允许用户仅上传5个文件。由于动态ajax删除,我没有得到记录的实时计数。上面的代码在删除记录时效果很好。现在我想返回数据库中的剩余行。
所以在模特中,我试图使用这样的东西,
public function delete_gallery_image($id){
$this->db->delete('employer_gallery', array('id' => $id));
$sql = " SELECT * FROM (`employer_gallery`) where user_id = $id";
$query = $this->db->query ( $sql );
return $query->num_rows ();
}
如果我得到一个计数,我可以将它分配给变量$ maxupload。这样我就可以对文件选择进行一些验证。但上面的模型不起作用,它给了我500个内部服务器错误。
有什么方法可以解决这个问题吗?
答案 0 :(得分:0)
function delete_gallery_image($id) {
$this->db->delete ( 'employer_gallery', array (
'id' => $id
) );
/* Flush the DB cache */
$this->db->flush_cache ();
/* $sql = " SELECT * FROM (`employer_gallery`) where user_id = $id"; */
$this->db->select ( '*' );
$this->db->from ( 'employer_gallery' );
return $rowcount = $query->num_rows ();
}