mysql - Deleting comments from database using php -
i making forum webpage i'll put delete buttons under each comment. when press button, send id of comment php file using ajax looks this:
function deletethis(index) { var result = confirm("want delete?"); if (result==true) { $.ajax({ url: "deletepost.php", type: "post", data: index, success: function(){ location.reload(); } }); } else return false; }
now problem can't receive php end. i've used debugger , saw index value right. php looks this:
<?php $con = mysqli_connect("localhost", "root", "123", "test") or die("die"); if (isset($_post['index'])) { $soontobedeletedcomment = $_post['index']; }; $index = intval($soontobedeletedcomment); mysqli_query($con, "delete commentsbox commentsbox.`id` = $index"); echo "post deleted..."; mysqli_close($con); ?>
my code doesnt give errors doesn't delete post either. when same process manually on navicat, working thought maybe index string , should integer. used intval didnt solve problem either. ideas me improve code appreciated. in advance
in jquery's .ajax call, data
property needs object, not string (string it's full query string, actually, it's not need here).
so, try this:
$.ajax({ url: "deletepost.php", type: "post", data: {id: index}, success: function(response){ alert(response); // location.reload(); } });
and in php, as:
$_post['id']
update
sanity checklist:
- is
deletethis
function receiving index? - is ajax calling right script?
- is ajax data property object, explained above?
- what output of php script?
- what contents of
$_post
? - is id inside
$_post
array ($_post['id']
)? - does row id exist in db?
these questions should pinpoint problem more accurately.
Comments
Post a Comment