Error inserting Data with Angularjs

Asked

Viewed 49 times

0

I am encountering errors when entering data with Angularjs

inserir a descrição da imagem aqui

Codigo Angular:

 var myapp = angular.module('myapp',[]);

        myapp.controller('productController',function($scope,$http) {

//listar produtos
          $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();
?>

System hosting

http://crudangularjsphp.azurewebsites.net/

1 answer

4


This was long obsolete and was removed in the current versions of Angularjs. See official documentation.

Now we need to use .then passing as first parameter the callback if the request is successful and as the second parameter the callback if there is an error (replacing the error).

$http.post("insert.php", seuObjeto).then(function() {
    console.log("Data Inserted Successfully");
});
  • I’m using angular.js 1.6 and have:

  • I used this code but it didn’t work:

  • $http.post("Insert.php", ' 'name':$Scope.name, 'price':$Scope.price, 'Quantity':$Scope.Quantity}). then(Function() { console.log("Data Inserted Successfully"); });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.