首页 > 代码库 > angularjs连接数据库查找删除操作

angularjs连接数据库查找删除操作

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>

<body>
<div ng-app="myApp" ng-controller="myctrl">
<table width=‘100%‘ border="1" cellspacing="0">
<tr>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>操作</th>
</tr>
<tr ng-repeat="x in list">
<td ng-bind=" x.username"></td>
<td ng-bind=" x.userage"></td>
<td ng-bind=" x.usersex"></td>
<td><input type="button" value="http://www.mamicode.com/删除" uid={{x.userid}} ng-click="del($event)"><input type="button" value="http://www.mamicode.com/修改" uid={{x.userid}}></td>  //uid是自己定的属性 并赋值    del(传自己为对象进去) 相当于js 的this
</tr>
</table>
</div>
</body>
<script src="http://www.mamicode.com/js/angular.min.js" charset="utf-8"></script>
<script type="text/javascript">
var app=angular.module(‘myApp‘,[]);
app.controller(‘myctrl‘,function($scope,$http){
$http({
method:‘GET‘,
url:"php/angu_test_sel.php"
}).then(function successCallback(response){
$scope.list=response.data.msg;
},function errorCallback(response){
alert("数据请求失败");
});


$scope.del=function(event){
var id=event.target.getAttribute("uid");//获取当前对象的uid值

$http({
method:‘GET‘,
params :{ id : id},//第一个id是后台php接受的参数名称  第二个id是本页面获取的删除的id值
url:"php/angu_test_del.php"
}).then(function successCallback(response){
window.location.reload();
},function errorCallback(response){
alert("删除失败");
});
}
})
</script>

 

 

<?php
header(‘Content-Type: text/html; charset=utf8‘);
$conn=mysql_connect("localhost","root","root");
mysql_query("use txt");
if($conn){
if($_GET){
$uid=$_GET["id"];
$sql="delete from test1 where userid=$uid";
$res=mysql_query($sql);
if($res>0){
echo ‘{"status":"1"}‘;
}else{
echo ‘{"status":"0"}‘;
}
}
}else{
echo ‘{"status":"-2"}‘;
}
?>

 

<?php
header(‘Content-Type: text/html; charset=utf8‘);
$conn=mysql_connect("localhost","root","root");
mysql_query("use txt");
if($conn){
$sql="select * from test1";
$res=mysql_query($sql);
if(mysql_num_rows($res)>0){
while($row=mysql_fetch_assoc($res)){
$arr[]= $row;
}
echo ‘{"status":"1","msg":‘.json_encode($arr).‘}‘;
}
}else{
echo ‘{"status":"-2";"msg":"fail to connect"}‘;
}
?>

 

angularjs连接数据库查找删除操作