php - 为什么我的数据库更新在没有刷新的情况下无法加载

标签 php mysql angularjs

我正在通过 php 将 angular 与 MySQL 结合使用。当我尝试添加到数据库时,它会添加,但我必须在浏览器上单击刷新才能查看结果。

我觉得这是 Angular 的真正好处,但我对 Angular 和 MySQL 也很陌生,所以我真的不知道我哪里出错了,也不知道如何让它正常工作。这似乎是我所有的数据库调用。

为了清楚起见,我在上面的页面上使用了 bootstrap、angular 和 jquery,并使用了 ng-include。

感谢任何能为我指明正确方向的人。

-html

<div class="widget-box" id="recent-box" ng-controller="ideasController">
    <div class="widget-header header-color-blue">
        <div class="row">
            <div class="col-sm-6">
                <h4 class="bigger lighter"><i class="glyphicon glyphicon-align-justify"></i>&nbsp;Idea MANAGER</h4>
            </div>
        </div>
    </div>
    <div class="widget-body ">
        <form id="newIdeaForm" class="add-idea">
        <div class="form-actions">
        <div class="input-group">
            <input type="text" class="form-control" name="comment" ng-model="ideaInput" placeholder="Add New Idea" >
        <div class="input-group-btn">
            <button class="btn btn-default" type="submit" ng-click="addIdea(ideaInput)"><i class="glyphicon glyphicon-plus"></i>&nbsp;Add New Idea</button>
        </div>
        </div>
        </div>
        </form>


        <div class="idea">
            <div style="width: 80%; margin-left:auto; margin-right:auto;">
                <table class="table table-hover"> 
                    <tbody> 
                        <tr ng-repeat="idea in ideas | orderBy:'idea'"> 
                            <td>{{idea.IDEA}} <a ng-click="deleteIdea(idea.ID)" style="float:right;"><button class="btn btn">Delete</button></a></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

-php

//addIdea.php

    <?php 
    require_once 'db.php'; // The mysql database connection script
    if(isset($_GET['idea'])){
    $task = $_GET['idea'];
    $status = "0";
    $created = time();
    $query=mysql_query("INSERT INTO ideas(idea,status,created_at)  VALUES ('$idea', '$status', '$created')") or die(mysql_error());
    }
    ?>

//getIdea.php

    <?php 
    require_once 'db.php'; // The mysql database connection script
    $status = '%';
    if(isset($_GET['status'])){
    $status = $_GET['status'];
    }
    $query=mysql_query("select ID, IDEA, STATUS from ideas where status like '$status' order by status,id desc") or die(mysql_error());

    # Collect the results
    while($obj = mysql_fetch_object($query)) {
        $arr[] = $obj;
    }

    # JSON-encode the response
    echo $json_response = json_encode($arr);
    ?>

-js

//Define an angular module for our app
var app = angular.module('myApp', []);

app.controller('ideasController', function($scope, $http) {
  getIdea(); // Load all available ideas 
  function getIdea(){  
  $http.get("ajax/getIdea.php").success(function(data){
        $scope.ideas = data;
       });
  };
  $scope.addIdea = function (idea) {
    $http.get("ajax/addIdea.php?idea="+idea).success(function(data){
        getIdea();
        $scope.ideaInput = "";
      });
  };

最佳答案

您需要刷新页面或页面的部分以从数据库中提取新信息。您可以使用 header通过您的 addIdea.php 重新加载页面。

   header('Location: thispage.html');

您可以在 INSERT 之后重新加载页面,新数据将在表中。

    <?php 
    require_once 'db.php'; // The mysql database connection script
    if(isset($_GET['idea'])){
    $task = $_GET['idea'];
    $status = "0";
    $created = time();
    $query=mysql_query("INSERT INTO ideas(idea,status,created_at)  VALUES ('$idea', '$status', '$created')") or die(mysql_error());
    }
      header('Location: idea.html');
    ?>

关于php - 为什么我的数据库更新在没有刷新的情况下无法加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23277126/

相关文章:

php - 为什么查询遗漏了第一行?

mysql - 如何使用 MySQL 或批处理文件创建文件名,其中部分文件内容需要在名称中?

MySql 索引排序和位置

java - 尝试从 AngularJS 到 Spring MVC 使用 $http.post 服务时出现错误 415 - Maven

javascript - Angular 翻译 : LocalStorage plugin and requirejs error

php - 来自有序查询的Mysql查询

php - 单击时将日期和时间保存到 mysql

javascript - Laravel 如何根据查询按天渲染成融合图表

MySql 跳过找到的第一条记录

angularjs - 向 logstash 发出发布请求时出现 CORS 错误