Saving to the database

-1

I need to save a form to the bank, with several input fields. Someone has an example of how you would pass the fields. I am using the framework AngularJS (If you need to transform them into an array or use json encode). This is the part of the view that contains the form. Doubt is like sending preco1 and nome1 to the bank. The file with the parameters and the 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>
    
asked by anonymous 12.06.2015 / 17:38

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');   }
    
15.06.2015 / 19:26
-1

In your Slim route file:

$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();
    
13.06.2015 / 02:59