在Web应用程序上工作,我刚刚添加了以下更新代码,但它不起作用。以下所有代码的摘要是:
- 单击名为
update
的按钮 - 它带出应包含点击/当前产品值的表单。
- 现在,当我以这种形式点击保存时,它应该更新数据库,但事实并非如此。
- 我正在使用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>
像下面这样做
您的视图部分
<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);
}
}
?>
我想你可能基本的想法,现在你可以按照你的方式实现。
尝试在 HTML 中使用 ng-model="{{product.name}}}"
而不是占位符。在控制器中传递该模型:
$http.post("update.php?action=update_entry",$scope.product)
然后你应该在你的PHP中得到一些数据。
您是否单独检查了 php,以确保您可以使用 php 获取和更新数据而无需 angular?我会使用 post,因为它对检索和更新数据更友好。
我还会将您对 php 端点的调用分离到服务(工厂)中。我也会将整个对象传回去,以确保您不会丢失某些内容,除非您担心带宽。
我会先对 php 进行单元测试。然后用角度分离逻辑。然后 b 在调试中单步执行以查看从视图传递的内容。
我认为你应该检查一下:https://github.com/eliarms/CustomerManagerApp
这是一个使用Angularjs和PHP的简单客户管理应用程序。该应用程序的目标是突出AngularJS提供的许多不同功能,并演示如何将它们一起使用。