【问题标题】:cant get the values from database in angularjs,php,mysql无法从 angularjs、php、mysql 中的数据库中获取值
【发布时间】:2018-10-03 22:50:43
【问题描述】:

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

这是我的 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);
?>

【问题讨论】:

    标签: php jquery mysql angularjs


    【解决方案1】:

    更新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);
    ?>
    

    【讨论】:

    • 它只创建表格框,但无法显示数据库中的值
    • 可以获取数据库值吗???在onSuccess(data){内的$scope.displayProduct函数中添加console.log(data),并检查
    • 不显示fatel错误。是查询问题吗??
    • {data: "
      致命错误:无法使用 [] 进行读取...cs\Angular\select.php 在线 8
    • 现在我可以获取数据库值 ^_^ 但它无法显示在表中。它显示一个空表
    猜你喜欢
    • 2011-07-30
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多