2016-08-04 4 views
-2

데이터베이스에서 쿼리를 삭제하려면이 테이블에 삭제 버튼을 추가하고 싶습니다. 나는 여기 Ajax와 PHP 코드가 있습니다jQuery를 사용하여 데이터베이스에서 레코드 삭제

<?php 
     require_once 'dbconfig.php'; 

     $stmt = $pdo->prepare("SELECT * FROM test"); 
     $stmt->execute(); 
    while($row=$stmt->fetch(PDO::FETCH_ASSOC)) 
    { 
    ?> 
    <tr> 
    <td><?php echo $row['id']; ?></td> 
    <td><?php echo $row['server']; ?></td> 
    <td><?php echo $row['reference']; ?></td> 
    <td align="center"><a id="<?php echo $row['id']; ?>" class="delete-link" href="#" title="Delete"> 
    <img src="images/delete.png" width="20px" /> 
      </a></td> 
    </tr> 
    <?php 
    } 
    ?> 

아약스 :

$(document).ready(function(){ 

/* Data Delete Starts Here */ 
$(".delete-link").click(function() 
{ 
    var id = $(this).attr("id"); 
    var del_id = id; 
    var parent = $(this).parent("td").parent("tr"); 
    if(confirm('Sure to Delete ID no = ' +del_id)) 
    { 
    $.post('delete.php', {'del_id':del_id}, function(data) 
    { 
    parent.fadeOut('slow'); 
    }); 
    } 
    return false; 
}); 
/* Data Delete Ends Here */ 

}); 

delete.php :

선택한 행이 보이지 않는다

는 DB에서 삭제하려면

<?php 
include_once 'dbconfig.php'; 

if($_POST['del_id']) 
{ 
$id = $_POST['del_id']; 
$stmt=$db_con->prepare("DELETE FROM test WHERE id=:id"); 
$stmt->bindParam(':id', $id); 
$stmt->execute(); 
} 
?> 
... 나는 아무런 오류도 없다.

+0

데이터 : {id : id}, 이와 같은 데이터를 전달하십시오. 제발 –

+0

이봐, 미안하지만 이해가 안돼 ... 좀 더 노골적이 될 수 있니? – Xibition

답변

0

데이터를 잘못 전달했습니다. 시도해보십시오.

$(document).ready(function() 
{ 
    $('table#delTable td a.delete').click(function() 
    { 
     if (confirm("Are you sure you want to delete this row?")) 
     { 
      var id = $(this).parent().parent().attr('id'); 
      var data = 'id=' + id ; 
      var parent = $(this).parent().parent(); 

      $.ajax(
      { 
        type: "POST", 
        url: "view.php", 
        data: {id:id}, 
        cache: false, 

        success: function() 
        { 
        parent.fadeOut('slow', function() {$(this).remove();}); 
        } 
      }); 
     } 
    }); 

}); 
+0

여전히 데이터베이스에서 삭제하지 않습니다 :/ – Xibition

+0

콘솔에 게시물 요청을 보았습니까? –

+0

아이디 값을 가지고있는 아이디를 확인하기 만하면됩니다. –

관련 문제