如何使用AngularJS更新/编辑数据库中的数据

时间:2016-02-11 13:13:46

标签: javascript php angularjs

在网络应用上工作时,我刚刚添加了以下更新代码,但它无效。 以下所有代码的摘要如下:

  • 点击名为update
  • 的按钮
  • 它会显示FORM,其中应包含已点击/当前产品的值。
  • 现在,当我点击以这种形式保存时,它应该更新数据库,但事实并非如此。
  • 我在PHP文件(update.php)中使用$_GET来获取当前产品ID。然后通过ID获取该产品的所有数据。

PS:控制台没有错误。

更新代码:

<?php 
    include "includes/connection.php";
    switch($_GET['action']) {
        case 'update_entry' :
            $data = json_decode(file_get_contents("php://input"));
            $index = $data->id; 
            $productname = $data->pname;
            $company = $data->company;
            $price = $data->price;
            $quantity = $data->quantity;
            if(isset($productname) && !empty($productname) && isset($company) && !empty($company) && isset($price) && !empty($price) && isset($quantity) && !empty($quantity)){
                $query = "UPDATE `product` SET `id`='$index',`name`='$productname',`company`='$company',`price`='$price',`quantity`='$quantity' WHERE id= $index";

                if(mysqli_query($con, $query)) {
                  return true;
                } else {
                  echo "Error: " . $sql . "<br />" . mysqli_error($con);
                }
                break;
            }   
    }
?>

控制器

myApp.controller("updateCtrl",['$scope','$http','$routeParams','$location',function($scope,$http,$routeParams,$location){
    $scope.update = function(){
         var currentId = $routeParams.id;
         $http.post("update.php?action=update_entry",{'id':currentId})
             .then(function(data){
                $location.path('/viewproduct');
         });
    }
}]);

HTML:

<form style="padding:10px" ng-controller="updateCtrl">
    <div class="form-group">
        <label for="ProductName">Product Name</label>
        <input type="text" class="form-control" placeholder="{{product.name}}" ng-model="productname" required>
    </div>
    <div class="form-group">
        <label for="company">Company </label>
        <input type="text" class="form-control" placeholder="{{product.company}}" ng-model="company" required>
    </div>
    <div class="form-group">
        <label for="company">Price </label>
        <input type="text" class="form-control" placeholder="{{product.price}}" ng-model="price" required>
    </div>
    <div class="form-group">
        <label for="company">Quantity </label>
        <input type="text" class="form-control" placeholder="{{product.quantity}}" ng-model="quantity" required>
    </div>
    <button type="submit" class="btn btn-default" ng-click="update()">Save updated data</button>
    <a href="#/viewproduct" class="btn btn-danger">Cancel</a>
    <h1 ng-if="successMessage == 0">Great Data is Updated!</h1>
</form>

更新按钮:

<td ng-controller="updateCtrl"><a class="btn btn-primary" href="#/updateproduct/action={{product.id}}" >Update</a></td>     

4 个答案:

答案 0 :(得分:3)

如下所示

您的观看部分

<form style="padding:10px" ng-controller="updateCtrl">
    <div class="form-group">
        <label for="ProductName">Product Name</label>
        <input type="text" class="form-control" placeholder="{{product.name}}" ng-model="productname" required>
    </div>
    <div class="form-group">
        <label for="company">Company </label>
        <input type="text" class="form-control" placeholder="{{product.company}}" ng-model="company" required>
    </div>
    <div class="form-group">
        <label for="company">Price </label>
        <input type="text" class="form-control" placeholder="{{product.price}}" ng-model="price" required>
    </div>
    <div class="form-group">
        <label for="company">Quantity </label>
        <input type="text" class="form-control" placeholder="{{product.quantity}}" ng-model="quantity" required>
    </div>
    <button type="submit" class="btn btn-default" ng-click="update()">Save updated data</button>
    <a href="#/viewproduct" class="btn btn-danger">Cancel</a>
    <h1 ng-if="successMessage == 0">Great Data is Updated!</h1>
</form>
<td><a class="btn btn-primary" ng-click="addProductData();" >Update</a></td> 

你的控制器内部如下所示

$scope.addProductData=function(){
   var updatedata=$.param({'action':'update','productname':$scope.productname,'company':$scope.company,'price':$scope.price,'quantity':$scope.quantity,'id':currentId});
    $http({
            method:'POST',
            url:'update.php',
            data:updatedata,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function successCallback(response){
            alert(response.data['msg']);
        },function errorCallback(response) {
            alert(response.data['msg']);
        });
}

您的update.php文件应如下所示。

<?php
include "includes/connection.php";
$result=array();
if(isset($_REQUEST["action"]) && $_REQUEST["action"] !=""){
   if($_REQUEST["action"]=="update"){
       $productname = $_POST['productname'];
       $company = $_POST['company'];
      $price = $_POST['price'];
       $quantity = $_POST['quantity'];
     $id=$_POST['id'];

     $query = "UPDATE `product` SET `name`='$productname',`company`='$company',`price`='$price',`quantity`='$quantity' WHERE id= $id";
   if(mysqli_query($con, $query)) {
    $result['msg']="updated successfully";
}else{
   header("HTTP/1.0 401 Unauthorized");
   $result['msg']="unable to updated";
}
echo json_encode($result);

}
}

?>

我认为你可能是基本的想法。现在你可以按照自己的方式实施。

答案 1 :(得分:1)

尝试使用ng-model="{{product.name}}}"而不是HTML中的占位符。 并在您的控制器中传递该模型:

$http.post("update.php?action=update_entry",$scope.product)

然后你应该在你的PHP中获得一些数据。

答案 2 :(得分:0)

你有没有检查过你的php,以确保你可以使用没有角度的PHP获取和更新数据?我会使用post,因为它更友好地检索和更新数据。

我还会将您对php端点的调用分成服务(工厂)。我也只是将整个对象传回去,以确保你不会遗漏某些东西,除非你对带宽有所顾虑。

我会首先对php进行单元测试。然后以角度分开逻辑。然后b在调试中单步执行以查看从视图中传递的内容。

答案 3 :(得分:0)

我认为你应该检查一下:https://github.com/eliarms/CustomerManagerApp
这是一个使用Angularjs和PHP的简单客户管理应用程序。该应用程序的目标是突出AngularJS提供的许多不同功能,并演示它们如何一起使用。