Saving in the database

Asked

Viewed 585 times

-1

I need to save a form in the bank, with several fields input. Someone has an example of how to pass the fields. I am using the framework Angularjs (If I need to turn them into one array or use json Encode). This is the part of view that contains the form. Doubt is how to send preco1 and nome1 to the database. The parameter file and configuration is Ok

<script>
        angular.module("slimApp", []);
        angular.module("slimApp").controller("slimAppController", function($scope, $http){
                $scope.app = "Slim Produtos";
                $scope.adicionarProduto = function(produtos){
                    $http.post('http://localhost/SlimProjeto/RestProdutos/produtos', {nome1:'', preco1:''}).
                      success(function(data, status, headers, config) {
                      }).
                      error(function(data, status, headers, config) {
                      });
                };
        });
    </script>
</head>
<body ng-controller="slimAppController">
    <div class="jumbotron">
        <h3>{{app}}</h3>
        <form class="form-control" method="post" id="form">
            <input type="text" id="nome1" name="nome1"/>
            <input type="text" id="preco1" name="preco1"/>
            <button class="btn btn-block btn-primary" ng-click="adicionarProduto(form)" >Adicionar Produto</button>
       </form>
    </div>
  • instead of sending each object item, send the whole object.

  • Here is an example: http://answall.com/questions/82975/databasecom-angularjs-e-php-n%C3%A3o-funciona/83162#83162

2 answers

0

<?php 
require '../Slim/Slim/Slim.php';
\Slim\Slim::registerAutoloader(); $app = new \Slim\Slim();
$app->response()->header('Content-Type',
'application/json;charset=utf-8');

$app->get('/', function () { echo "SlimProdutos"; });
$app->post('/produtos','addProduto');
$app->GET('/produto','adcProduto');

$app->run();

function getConn() { return new
PDO('mysql:host=localhost;dbname=slimprodutos', 'root', '',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") ); }

function addProduto() { $request =
\Slim\Slim::getInstance()->request(); $produto =
json_decode($request->getBody()); $sql = "INSERT INTO produtos
(nome, preco, idCategoria) values (:nome, :preco, 1) "; $conn =
getConn(); $stmt = $conn->prepare($sql);
$stmt->bindParam("nome",$produto->nome);
$stmt->bindParam("preco",$produto->preco); $stmt->execute();
$produto->id = $conn->lastInsertId(); echo json_encode($produto); };
function adcProduto(){  echo('teste');   }

-1

In the archive of your Slim route:

$app = new \Slim\Slim();

$app->post('/url_do_post', function () {

    // Pegue as variaveis 

    $request = \Slim\Slim::getInstance()->request();
    $data = $request->getBody();


    // Função para conexão com banco

    function getConn()
    {
        return new PDO(
            'mysql:host=localhost;dbname=nome_banco',
            'usuario_banco',
            'senha_banco',
            array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
        );
    }

    // Para executar queries

    $sql = "INSERT INTO tabela SET coluna1=:valor1, coluna2=:valor2";
    $conn = getConn();
    $query = $conn->prepare($sql);
    $query->bindParam("coluna1", $data['valor1']);
    $query->bindParam("coluna2", $data['valor2']);
    $query->execute();
});
$app->run();
  • It worked differently. It follows below the file of the route, was generating a query $products, and was forgetting only flames the attributes of it.

Browser other questions tagged

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