Error inserting Data with AngularJS

0

I'm finding errors when entering data with AngularJS

AngularCode:

varmyapp=angular.module('myapp',[]);myapp.controller('productController',function($scope,$http){//listarprodutos$http.get("customers_mysql.php").then(function (response) {$scope.listProducts = response.data.records;});

//inserir produtos
              $scope.add = function(){

                    /*$scope.listProducts.push({
                        id:$scope.id,
                        name:$scope.name,
                        price:$scope.price,
                        quantity:$scope.quantity
                    });*/

                    $http.post("insert.php", {

                        'name':$scope.name,
                        'price':$scope.price,
                        'quantity':$scope.quantity})                    
                        .success(function(data,status,headers,config){
                            console.log("Data Inserted Successfully");
                        });

                    $scope.id       = '';
                    $scope.name     = '';
                    $scope.price    = '';
                    $scope.quantity = '';
                };

PHP Code

 <?php  
    $HOST  = "localhost";
    $LOGIN = "root";
    $SENHA = "";

    mysql_connect( $HOST, $LOGIN, $SENHA) or die("Não foi possível a conexão com o servidor");
    mysql_select_db("bancoteste") or die("Não foi possível selecionar o banco de dados");

    $data       = json_decode(file_get_contents("php://input"));
    $name       = mysql_real_escape_string($data->name);
    $price      = mysql_real_escape_string($data->price);
    $quantity   = mysql_real_escape_string($data->quantity);

    $sql = "INSERT INTO tprodutos('name', 'price', 'quantity') VALUES('".$name."','".$price."','".$quantity."')";
    $result = mysql_query($sql);


    mysql_close();
?>

Hosting the system

link

    
asked by anonymous 20.07.2017 / 19:36

1 answer

4

This was out of date for some time and has been removed in current versions of AngularJS. See official documentation .

Now it is necessary to use .then passing as the first parameter the callback in case the request is successful and as a second parameter the callback in case of an error (replacing error ).

$http.post("insert.php", seuObjeto).then(function() {
    console.log("Data Inserted Successfully");
});
    
20.07.2017 / 19:50