2013-10-09 2 views
-1

'delete'라는 ID가있는 버튼을 클릭하면 데이터베이스에서 행을 삭제해야하는 PHP 스크립트를 호출하기 위해 ajax 메서드를 사용하고 있습니다.PHP에서 MySQL DELETE 쿼리가 실행되지 않습니다.

$('#delete').click(function() { 
    var id = parseInt($('#content').attr('data-name')); 
    // The row in my database has the type integer 
    // Doing alert(id) correctly returns the value 

$.ajax({ 
    url : './php/delete.php', 
    type: 'POST', 
    data : id, 
    success: function() 
    { 
     alert('success deleting ' + id); 
     location.reload(); 
     // These both get called correctly, but the row is not actually getting deleted 
    } 
}); 

}); 

내 PHP 코드에 phpMyAdmin에서 같은 쿼리를 실행

<?php 
$con=mysqli_connect("localhost","root","tulula15","content_test"); 

if (mysqli_connect_errno()) 
    { 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

mysqli_query($con,"DELETE FROM htmlcontent WHERE id='$_POST[id]'"); 

mysqli_close($con); 
?> 

입니다 제대로

DELETE FROM htmlcontent WHERE id=2 

그래서 아약스가 성공적으로 반환하는 행을 삭제하지만, 쿼리가 실행되지 않습니다.

누군가가 제가 매우 감사하겠습니다이 문제를 해결하는 데 도움 수 있다면

..

+5

당신은 SQL 주입에 높게 취약합니다. –

+0

1. 쿼리를 실행할 때 mysqli 오류 처리를 사용하고 2. 서버 로그 파일을 살펴보십시오. – arkascha

+0

prepare 문을 사용하여 $ _POST [ 'id'] 변수를 매개 변수화하십시오. – aarryy

답변

4

AJAX 호출에서 데이터를 제대로 설정하지 않았습니다. 이것을하십시오 :

$.ajax({ 
    url : './php/delete.php', 
    type: 'POST', 
    data : {id:id},   // use a map like this to set up the POST data 
    success: function() 
    { 
    alert('success deleting ' + id); 
    location.reload(); 
    // These both get called correctly, but the row is not actually getting deleted 
    }  
}); 
+0

고마워, 이걸 해결했다 –

+0

"bye bye htmlcontent"는 아직 실제라고 생각합니다. ( –

-3

귀하의 질의는 옳지 않다. 끈을 탈출하지 않았습니다. 다음과 같이해야합니다. 또한 변수를 문자열에 연결하지 않았습니다.

mysqli_query($con,"DELETE FROM htmlcontent WHERE id='".$_POST[id]."'") 
+0

You * still *은 문자열을 이스케이프 처리하지 않는다. 준비된 문을 사용하여 SQL 주입을 방지하십시오. –

+0

$ _POST [ 'id']은 (는) ''OR '1'= '1'과 (과) 일치합니까? –

+0

@vp_arth 안녕히 by htmlcontent –

관련 문제