如何使用Angularjs在mysql中获取特定id的数据

我有一个基于 PHP angularjs 的 Web 界面,当他们使用 angular 登录时,我应该获取特定用户的数据进行编辑


我所能做的就是 ng-repeat 获取所有 id 的所有行


控制器.js文件


var crudApp = angular.module('crudApp',[]);

crudApp.controller("DbController",['$scope','$http', function($scope,$http){


  // Function to get employee details from the database

  getInfo();

  function getInfo(){

    // Sending request to EmpDetails.php files

    $http.post('databaseFiles/Details.php').success(function(data){

      // Stored the returned data into scope

      $scope.details = data;

    });

  }

index.php 文件


<tr ng-repeat="detail in details| filter:search_query">

    <td>{{$index + 1}}</td>

    <td>{{detail.trainer_name}}</td>

    <td>{{detail.trainer_team}}</td>

    <td>{{detail.trainer_code}}</td>

    <td>

        <button class="btn btn-warning" ng-click="editInfo(detail)" title="Edit"> 

            <span class="glyphicon glyphicon-edit"></span>

        </button>

    </td>

    <td>

        <button class="btn btn-danger" 

                ng-click="deleteInfo(detail)" title="Delete">

            <span class="glyphicon glyphicon-trash"></span>

        </button>

    </td>

</tr>

详情.php


<?php

// Including database connections

require_once 'database_connections.php';

// mysqli query to fetch all data from database

$query = "SELECT * from pokedstats";

$result = mysqli_query($con, $query);

$arr = array();

if(mysqli_num_rows($result) != 0) {

while($row = mysqli_fetch_assoc($result)) {

$arr[] = $row;

}

}

// Return json array containing data from the databasecon

echo $json_info = json_encode($arr);

?>

我只想显示与特定 id 相关的行,但我只能获取所有行值并显示它们并对其进行编辑。我只希望具有唯一 ID 的特定用户只能编辑行数据或将其删除。


慕哥6287543
浏览 90回答 1
1回答

海绵宝宝撒

if在您的editInfo和deleteInfo方法中使用语句来检查用户的权限。这是一些伪代码,可以为您提供想法:$scope.permissionLevelRequired = 3;$scope.userPermission = 1; // Get this from the database instead$scope.editInfo(detail) {&nbsp; if ($scope.userPermission >= $scope.permissionLevelRequired) {&nbsp; &nbsp; // Make changes to `detail`&nbsp; &nbsp; ...&nbsp; }}$scope.deleteInfo(detail) {&nbsp; if ($scope.userPermission >= $scope.permissionLevelRequired) {&nbsp; &nbsp; // Make changes to `detail`&nbsp; &nbsp; ...&nbsp; }}这只是客户端验证,它适用于您的界面目的,但很容易被黑客入侵。在更改数据库之前,您可能还想验证服务器上的权限。
打开App,查看更多内容
随时随地看视频慕课网APP