ajax实现删除数据库中的数据

时间:2022-10-16 22:21:20

html代码

<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<script type="text/javascript" src="js/jquery.js"></script>

<title>Document</title>
</head>

<style type="text/css">
table.imagetable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.imagetable th {
background:#b5cfd2 url('cell-blue.jpg');
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
table.imagetable td {
background:#dcddc0 url('cell-grey.jpg');
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
</style>
<body>
<table class="imagetable">
<tr>
<th>id</th>
<th>姓名</th>
<th>密码</th>
<th>性别</th>
<th>系部</th>
<th>班级</th>
<th>电话</th>
<th>操作</th> <!-- 添加一个操作列-->
</tr>
<?php


header( 'Content-Type:text/html;charset=utf-8 ');


include_once("conn/conn.php"); //引入导入数据库控制页面



$sql="SELECT*from user"; //查询数据库中的user;

$r=mysqli_query($link,$sql);

$row=mysqli_fetch_array($r); //查询出来的数组类型

while ($row=mysqli_fetch_array($r)){
/*print_r($row);
echo "<br>";*/
/* echo $row["name"]."=>".$row["mima"]; //查询姓名和密码 也可以用键来查询
echo "<br>";*/

?>

<tr>
<td><?php echo $row["id"]?></td>
<td><?php echo $row["name"]?></td>
<td><?php echo $row["mima"]?></td>
<td><?php echo $row["xingbie"]?></td> <!-- 从数据库中查询出的数据装着表格中-->
<td><?php echo $row["xibu"]?></td>
<td><?php echo $row["banji"]?></td>
<td><?php echo $row["tex"]?></td>
<td style="color: red;"><a href="" onclick="del( <?php echo $row["id"]?> );">删除</a></td> <!-- 在列中添加删除onclick实现跳转-->


</tr>


<?php
}
?>

<script>
function del($id) {

var id=$id;
if (id){
$.get("shanchu.php",{id:id},function (data) { /*get获取数据*/
if (data=201){
alert("成功");
window.location.href=window.location.href; /* 刷新*/
}else {
alert("失败");

}

});
}else {
alert("失败");
}
}






</script>

</table>
</body>
</html>

php代码
<?php
header( 'Content-Type:text/html;charset=utf-8 ');


include_once("conn/conn.php");

$id=$_GET["id"];
$sql="delete from USER WHERE id=$id";
$r=mysqli_query($link,$sql);
if ($r){
echo "201";
}else{
echo "200";
}