dtsjq28482 2015-09-21 05:35
浏览 90

使用angular.js和PHP从数据库中检索数据

I need to retrieve all my data from database and bind them in text-field using angular.js and PHP. I am explaining my code below.

index.php

<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD Operations With PHP and MySql - By Cleartuts</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<center>

<div id="body">
 <div id="content">
    <table align="center">
    <tr>
    <th colspan="5"><a href="add_data.html">add data here.</a></th>
    </tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>City Name</th>
    <th colspan="2">Operations</th>
    </tr>
    <?php
 $sql_query="SELECT * FROM users";
 $result_set=mysql_query($sql_query);
 while($row=mysql_fetch_row($result_set))
 {
  ?>
        <tr>
        <td><?php echo $row[1]; ?></td>
        <td><?php echo $row[2]; ?></td>
        <td><?php echo $row[3]; ?></td>
  <td align="center"><a href="edit_data.php?edt_id=<?php echo urlencode($row[0]) ?>"><img src="images/pencil_small.png" align="EDIT" /></a></td>
        <td align="center"><a href="javascript:delete_id('<?php echo $row[0]; ?>')"><img src="images/cross-small-icon.png" align="DELETE" /></a></td>
        </tr>
        <?php
 }
 ?>
    </table>
    </div>
</div>

</center>
</body>
</html>

When user will click on edit button it will redirect to the next page(i.e-edit_data.php) which is given below and this page contains all the data which are going to be edited.

edit_data.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="edit_data">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD Operations With PHP and MySql - By Cleartuts</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<script src="js/angularjs.js" type="text/javascript"></script>
<script src="js/update.js" type="text/javascript"></script>
</head>

<body>
<center ng-controller="updateController">
<div id="body">
 <div id="content">
    <table align="center">
    <tr>
   <td><input type="text" name="first_name" placeholder="First Name" ng-model="first_name"  required /></td>
    </tr>
    <tr>
    <td><input type="text" name="last_name" placeholder="Last Name" ng-model="last_name"  required /></td>
    </tr>
    <tr>
    <td><input type="text" name="city_name" placeholder="City" ng-model="city"  required /></td>
    </tr>
    <tr>
    <td>
    <button type="submit" name="btn-update" ng-click="update_data();"><strong>UPDATE</strong></button>
    </td>
    </tr>
    </table>
    </div>
</div>

</center>
</body>
</html>

In this page i need to display all fetched data inside text-field.

update.js:

var app=angular.module("edit_data", []);
app.controller("updateController",function($scope,$http){
    $scope.errors = [];
    $scope.msgs = [];
    $scope.update_data=function(){
        $http.post('js/update.php',{"first_name":$scope.first_name,"last_name":$scope.last_name,"city":$scope.city}
        ).success(function(data, status, headers, config){
            if(data.msg!=''){
            $scope.msgs.push(data.msg);
            }else{
                $scope.errors.push(data.error);
            }
        }).error(function(data, status) { // called asynchronously if an error occurs
               // or server returns response with an error status.
              $scope.errors.push(status);
        });
    }
});

I need to retrieve these three data first_name,last_name,user_city from my Mysql database.Please help me to resolve this issue.

  • 写回答

2条回答

  • duana1021 2015-09-21 08:52
    关注

    make php file that connect to db and retrieve your given parameter from db, and after that call it from angular :

    var app=angular.module("myApp", []);
    app.controller("updateController",function($scope,$http){
    $scope.errors = [];
    $scope.msgs = [];
    $scope.update_data=function(){
        $http.post('update.php',  {"first_name":$scope.first_name,"last_name":$scope.last_name,"city":$scope.ci    ty}
        ).success(function(data, status, headers, config){
            $scope.resultData = data;
      }).error(function(data, status) { // called asynchronously if an error occurs
               // or server returns response with an error status.
              $scope.errors.push(status);
        });
    }
    });
    

    view file:
    <div ng-controller="updateController" ng-app="myApp"> <div ng-repeat=item in resultData"> {{item.yourDbdata}} </div> </div>

    your update php get posted data call db and echo json_encode(query result)

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建