无法从angularjs,php,mysql中获取数据库中的值

时间:2018-04-23 19:38:50

标签: php jquery mysql angularjs

产品已成功插入但是 无法显示数据库中的产品 它没有显示任何错误...

这是我的main.html页面 首先在angularjs函数中有一些问题然后我将函数从Success()更改为then()然后值插入工作但现在无法从数据库加载数据并显示它

<!DOCTYPE html>
<html>
<head>
<title>Office Essentials</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<h2>Office Essentials</h2>
<div ng-app="myApp" ng-controller="cntrl">
<form>
        Product Name:<br>
        <input type="text" ng-model="name" name="name"><br>
        Quantity:<br>
        <input type="text" ng-model="quantity" name="quantity"><br>
        Price:<br>
        <input type="text" ng-model="price" name="price"><br>
        Details:<br>
        <textarea ng-model="details" name="details"></textarea>
        <br><br>
        <input type="submit" ng-click="insertData()" value="Submit">
        <br><br>
        <input type="submit" ng-click="displayProduct()" value="Show">
        {{msg}}
</form>

这是用于显示数据库中的产品

<table border="1">
  <tr>
    <th>Id</th>
    <th>Product Name</th>
    <th>Quantity:</th> 
    <th>Price</th>
    <th>Details</th>
    <th>Status</th>
  </tr>
  <tr ng-repeat="product in data">
    <td>{{product.id}}</td>
    <td>{{product.name}}</td>
    <td>{{product.quantity}}</td> 
    <td>{{product.price}}</td>
    <td>{{product.details}}</td>
    <td>{{product.status}}</td>
  </tr>
</table>
</div>

这是我在main.html页面中使用的sqript

<script >
    var app=angular.module('myApp',[]);
    app.controller('cntrl',function($scope,$http){
        $scope.insertData = function(){
            $http.post("insert.php",
            {
                'id':$scope.id,
                'name':$scope.name,
            'quantity':$scope.quantity,
            'price':$scope.price,
            'details':$scope.details
            })
            .then(function onSuccess(){
                $scope.msg = "data Inserted";
            })
        }

        $scope.displayProduct=function(){
            $http.get("select.php")
            .then(function onSuccess(data){
                $scope.data=data
            })
        }
    });
</script>

</body>
</html>

这是我的select.php页面

<?php
include "connectdb.php";

$query = "SELECT * FROM officeessentials";
$result=$dbhandle->query($query);

while($row = $result->fetch_assoc()){
$data[]->$row;
}

print json_encode($data);
?>

1 个答案:

答案 0 :(得分:0)

更新select.php

中的以下代码
<?php
include "connectdb.php";
$query = "SELECT * FROM officeessentials";
$result = $dbhandle->query($query);
$data = [];
while($row = $result->fetch_assoc()){
    $data[] = $row;
}
echo json_encode($data, true);
?>