5

Working on a web app , I just added the below update code and it's not working . The summary of all the below code is :

  • Click a Button called update
  • It brings out the FORM which should contain the values of the clicked/current product.
  • Now when I hit save in this form it should update the database but it is not.
  • I am using $_GET in PHP file (update.php) to get the current Product ID.And then getting all data of that product via that ID.

PS: There is no error in console.

UPDATE CODE:

<?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;
            }   
    }
?>

Controller :

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>

Update Button:

<td ng-controller="updateCtrl"><a class="btn btn-primary" href="#/updateproduct/action={{product.id}}" >Update</a></td>     
9
  • 1
    Can you see the data in your php? I'm not a PHP-Expert, but you POST the data in Angular and want to GET it in PHP, try $_POST instead maybe. In Addition your contoller does not have a model-object which seems strange as well. Commented Feb 11, 2016 at 13:23
  • @FKutsche $_GET is used to get data from URL so I am fetching that specific(current or clicked) ID and through that ID the data of the clicked product.$_POST is for posting data into something.php . Commented Feb 11, 2016 at 13:25
  • 1
    Your controller is still using $http.post instead of $http.get Commented Feb 11, 2016 at 13:29
  • @apex-meme-lord I am posting data to update.php not getting. Commented Feb 11, 2016 at 13:32
  • 1
    If your client sends a post request, PHP saves it in $_POST. You can also use $_REQUEST, but given key conflicts it will overwrite get parameters (query string). Commented Feb 11, 2016 at 14:06

4 Answers 4

3

Do like below

your view part

<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> 

Inside your controller do like below

$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']);
        });
}

your update.php file should like below.

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

}
}

?>

i think you may basic idea.now you can implement in your way.

Sign up to request clarification or add additional context in comments.

Comments

1

Try to use ng-model="{{product.name}}}" and not the placeholder in HTML. And in your controller pass that model:

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

Then you should get some data in your PHP.

6 Comments

if you pass the data with $http.post then you should get what is in your $scope.product. And adding the product to your ng-model and not the placeholder is where it should be. if you console.log($scope.product); right before http request you can see if there is any data there or not.
oh and use ng-submit="update()" on the form instead of have it on the button.
why not in placeholder I want the user to view the data which he wants to edit . And please can you explain what $scope.product will contain ?
when you connect the product object with ng-model in the html, you can get those values by accessing the $scope.product from the controller.
The solution works fine. do you want me to teach you why you must use ng-model and $scope or do you want me to make an example for you to try?.. And thanks for the downvote when someone acctualy tries to help.
|
0

Have you checked your php alone to make sure that you can fetch and update data using the php without angular? I would use post as it is more friendly for retrieving and updating data.

I would also b separate your call to the php endpoint into a service (factory). I would also just pass the entire object back through to ensure that you aren't missing something unless you have a concern about bandwidth.

I would unit test php first. Then separate logic in angular. Then b step through in debug to see what's being passed from the view.

Comments

0

I think you should check this: https://github.com/eliarms/CustomerManagerApp
This is a simple customer management app using Angularjs and PHP. The goal of the application is to highlight a lot of the different features offered by AngularJS and demonstrate how they can be used together.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.