我无法从数据库中删除记录,获取代码有效,但删除代码无效。请帮助我..提前致谢
使用ajax获取数据的代码
$(document).ready(function(){
done();
});
function done(){
setTimeout(function(){
updates();
done();
}, 200);
}
function updates(){
$.getJSON("fetch.php",function(data){
$("table").empty();
$("table").append("<tr><td>Name</td><td>Date</td><td>Delete</td></tr>");
$.each(data.result, function(){
$("table").append("<tr><td>"+this['text']+"</td><td>"+this['date']+"</td><td><a id='del' href='"+this['id']+"'>Del</a></td></tr>");
});
});
}
使用ajax删除数据的代码
$(function() {
$("#del").click(function(){
var element = $(this);
var id = element.attr("id");
var dataString = 'id=' + id;
if(confirm("Sure you want to delete this comment?"))
{
$.ajax({
type: "GET",
url: "del.php",
data: dataString,
success: function(){
}
});
}
return false;
});
});
php代码del.php
$last_page_id = $_REQUEST['d_i_d'];
$sql = mysql_query("delete from time where id = '{$last_page_id}'");
if(!$sql){
echo mysql_error();
}else{
header('location: index.php');
}
最佳答案
Ajax 数据:dataString = 'id=' + id;
在 php 中调用:$last_page_id = $_REQUEST['d_i_d'];
您可以通过$_REQUEST['id']
获取id
请注意 mysql_ 已弃用:Why shouldn't I use mysql_* functions in PHP?
并且您的代码对 SQL 注入(inject)开放:http://en.wikipedia.org/wiki/SQL_injection
关于javascript - 使用ajax删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26465335/