gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:34:10 24 4
gpt4 key购买 nike

这是我的代码的一部分,我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com