0
아래 코드는 컨트롤러에 추가 명령문을 추가 할 때만 데이터를 삭제합니다. 그게 왜?데이터베이스를 처리하는 올바른 방법은 무엇입니까?
나는 몇 달 동안 codeigniter를 사용 해왔다. 나는 이런 이상한 버그에 계속 빠져있다.
My_model.php
function delete_data($where=array())
{
return $this->db->delete('table1', $where);
}
와 컨트롤러의 코드 :
이
는 모델 파일입니다tasks.php
function do_delete_data()
{
$this->load->model('My_model');
$result = array('status' => '', 'message' => '');
try
{
$this->db->trans_begin();
$id = $this->input->post('post_id', TRUE);
if (! $this->My_model->delete_data(array('id' => $id)))
{
throw new Exception('Database process failed.');
}
$result['message'] = $this->db->last_query(); // extra statement
$this->db->trans_commit();
$result['status'] = 1;
}
catch(Exception $e)
{
$this->db->trans_rollback();
$result['message'] = $e->getMessage();
}
if ($this->input->is_ajax_request())
{
echo json_encode($result);
}
}
을 나는이 같은 아약스를 통해이 함수를 호출하려고 최근까지 잘 작동 :
display.php
$.ajax({
url: '/tasks/do_delete_data',
type: 'post',
data: {
'post_id' : $('#post_id').val(), // e.g. 12
},
dataType: 'json',
success: function(response) {
alert('File deleted successfully.');
console.log(response);
},
error: function(e) {
alert('an error occurred.');
}
});
u가'$ _POST'와 함께'$ id'를 사용해야합니다. – devpro
@devpro 죄송합니다. 메모리에서이 질문을 쓰고 코드를 잘못 확인한 후에 코드를 편집했습니다. 제발보십시오 – dapidmini
예, 잘못된 색인 이름을 사용하여 여기'$ id = $ this-> input-> post ('post_id', TRUE);''$ id = $ this-> input-> post (' 'id', TRUE);'print_r ($ this-> input-> post())'로 여분의 명령문을 대체했을 때 – devpro