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

标签 php jquery mysql ajax

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

文件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>

文件删除.php

<?php 

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

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




?>

最佳答案

var id = $(this).attr("id");

这是试图读取被点击按钮的 ID。您需要 tr 的 ID:

var id = row.attr("id");

你也不需要:first,你只需要包含按钮的tr:

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

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

关于php - 使用 jQuery 和 SQL 从表中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18168237/

相关文章:

mysql - 2个jsp表单保存到一个SaveServlet中

mysql - 在 MySQL 查询的 WHERE 子句中使用列别名会产生错误

php - mysql_real_escape_string 和 json

javascript - 即使一切正常,If 语句仍无法正常工作

php - 在表单上提交发送电子邮件+打开弹出窗口

mysql - 仅将时间戳字段与年份和月份进行比较

php - 通过php上传pdf文件到mysql时查询失败

php - 什么时候在 PHP 中对字符串使用增量运算符?

php - 数组,从 MySQLi 到 PDO

jquery - 从表中检索 tr