Error inserting Angular data using $http.post(). then

Asked

Viewed 104 times

-3

I cannot insert the data into mysql using Angularjs. The data is not sent to the Insert.php file. I believe there is an error $http.post that I am not aware of!

Any suggestions ?

System: http://crudangularjsphp.azurewebsites.net/index.html

inserir a descrição da imagem aqui

Codigo Script

//Metodo adicionar
            $scope.add = function(){

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

                /*inserir dados no banco*/
                $http.post("insert.php", {
                    name:$scope.name,
                    price:$scope.price,
                    quantity:$scope.quantity})                  
                    .then(function(data,status,headers,config){
                        console.log("Data Inserted Successfully");
                });

                //limpar os inputs
                $scope.id       = '';
                $scope.name     = '';
                $scope.price    = '';
                $scope.quantity = '';
            };

PHP code

<?php   

    $HOST  = "xxxxxxx";
    $LOGIN = "xxxx";
    $SENHA = "xxxxxx";
    $db = "localdb";

    mysql_connect($HOST, $LOGIN, $SENHA) or die("Não foi possível a conexão com o servidor");
    mysql_select_db($db) 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();
?>
  • Test is here worked out, the only thing missing is you return the ID of the object that was created on the PHP server;

  • You need to at least tell me why you can’t.

  • The id is autocriment in mysql. I did not pass because it is created automatically

  • Even if it is created automatically mysql manages to return the id, in real and good returns the entire Obejto, so the screen will represent the correct value of the database, without giving space for trickster pranks, rs

  • But I’m entering the data into the bank!

  • Even making the corrections the error persists!

  • Any more suggestions ?

Show 2 more comments

1 answer

0


Error in SQL syntax = $sql = "INSERT INTO tprodutos (id, name, price, quantity) VALUES (NULL, '". $name." ', '". $price." ', '". $Quantity."');";

Browser other questions tagged

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