【发布时间】:2016-05-22 06:55:27
【问题描述】:
在网络应用程序上工作,我刚刚添加了以下更新代码,但它不起作用。 以下所有代码的摘要是:
- 单击一个名为
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>
【问题讨论】:
-
你能看到你的php中的数据吗?我不是 PHP 专家,但是您在 Angular 中发布数据并想在 PHP 中获取它,也许可以尝试使用 $_POST。此外,您的控制器没有看起来也很奇怪的模型对象。
-
@FKutsche $_GET 用于从 URL 获取数据,因此我正在获取该特定(当前或点击的)ID,并通过该 ID 获取点击产品的数据。$_POST 用于将数据发布到某物.php .
-
您的控制器仍在使用
$http.post而不是$http.get -
@apex-meme-lord 我将数据发布到 update.php 没有收到。
-
如果您的客户端发送一个 post 请求,PHP 会将其保存在
$_POST中。您也可以使用$_REQUEST,但如果出现键冲突,它将覆盖获取参数(查询字符串)。
标签: javascript php angularjs