Php 使用jQuery和SQL从表中删除数据

Php 使用jQuery和SQL从表中删除数据,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,这是我代码的一部分,我正在使用ajax post和delete数据,一切正常,只是数据没有从数据库中删除。我做错了什么? 我们开始: File view.php <table class='uk-table uk-table-striped'><tbody> <thead> <tr> <th>Ano</th> <th>Grau</th> <th>Serie</th> <t

这是我代码的一部分,我正在使用ajax post和delete数据,一切正常,只是数据没有从数据库中删除。我做错了什么? 我们开始:

File view.php

<table class='uk-table uk-table-striped'><tbody>
<thead>
<tr>
<th>Ano</th>
<th>Grau</th>
<th>Serie</th>
<th>Curso</th>
<th>Instituição</th>
<th>Cidade</th>
<th>Estado</th>
<th>Excluir?</th>
</tr>
</thead>

<script type="text/javascript">
$(document).ready(function() {
$('#load').hide();
});

$(function() {
$(".delete").click(function() {


 if (confirm("Tem certeza?"))
                {
                    var row = $(this).parents('tr:first');
                    var id = $(this).attr("id");
                    var data = 'id=' + id ;

$.ajax({
   type: "post",
   url: "delete.php",
    data: data,
   cache: false,
   success: function(){ 
    row.slideUp('slow', function() {$(row).remove();});

  }


 });


 }

return false;
    });
});


echo "<tr id='".$historico['id']."'>";
echo "<td>".$historico['ano']."</td>";
echo "<td>".$grau['grau']."</td>";
echo "<td>".$serie['serie']."</td>";
echo "<td>".$curso['curso']."</td>";
echo "<td><a href='index.php?option=com_community&view=groups&task=viewgroup&groupid=".$grupo['id']."'>".$grupo['name']."</a></td>";
echo "<td>".$cidade['nome']."</td>";
echo "<td>".$estado['sigla']."</td>";
echo "<td><button class='delete uk-button uk-button-danger'>Delete</button></td>";
echo "</tr>";</tbody></table>

阿诺
格劳
塞列
库索
研究所
西达德
埃斯塔多
排除者?
$(文档).ready(函数(){
$('#load').hide();
});
$(函数(){
$(“.delete”)。单击(函数(){
如果(确认(“Tem certeza?”)
{
var行=$(this.parents('tr:first');
var id=$(this.attr(“id”);
变量数据='id='+id;
$.ajax({
类型:“post”,
url:“delete.php”,
数据:数据,
cache:false,
成功:函数(){
slideUp('slow',function(){$(row.remove();});
}
});
}
返回false;
});
});
回声“;
回声“$historico['ano']”;
回显“$grau['grau']”;
回声“$serie['serie']”;
回显“$curso['curso']”;
回声“;
回声“$cidade['nome']”;
echo“$estado['sigla']”;
回应“删除”;
回声“;
文件delete.php

<?php 

include 'con.php';
$id= $_POST["id"];

$query=mysql_query("DELETE FROM historico WHERE id = '$id'")or die(mysql_error());




?>

这正试图读取已单击按钮的id。您需要tr的id:

var id = row.attr("id");
您不需要
:首先,您只需要包含按钮的
tr

var row = $(this).parents('tr');

parent('tr')
应该有效,而不是
parent
。(家长可能可以工作,但没有必要。)

让事情变得更简单在每个删除按钮中打印id

echo "<td><button class='delete uk-button uk-button-danger' id='".$historico['id']."'>Delete</button></td>";
echo“删除”;
然后按原样使用代码

正如Andy G所说您必须删除或修改tr的id属性值


您的
id
列是varchar吗?你是用你的
引语以那种方式发送的。如果是整数,则不需要它们。此外,您将自己留给使用折旧方法的SQL注入。阅读php文档您是否进行过任何调试?确保您的php正在接收发布的变量,并且其内容是正确的。确保您的php可以连接到数据库并执行删除语句。您必须提供生成每个删除按钮的代码。delete button id属性应具有数据库中记录的id。mysql库已弃用,请使用mysqli或PDO,并准备好语句。打印
id
的值以检查其是否正确。您可以使用
console.log(id)并在浏览器控制台中阅读。无论如何,您都应该检查控制台是否有错误。当然,您已经附加了jQuery库?。请不要忘记从tr元素中删除此id。@AndyG请提供更多详细信息!我听不懂你的意思。id在页面上必须是唯一的。如果您不将其从tr中删除,则它将被复制。就我个人而言,我会把id留在tr元素上,因为这个元素无论如何都需要定位。安迪,你说得对,我的意思是,它就像你们现在说的那样工作。我只是在每个删除按钮上打印id。谢谢各位。
echo "<td><button class='delete uk-button uk-button-danger' id='".$historico['id']."'>Delete</button></td>";