Doubt on query with parameters using AngularJS and http request get method

0

First, good afternoon. I have come to ask for your help because I have spent some hours looking for a solution and nothing.

I'm trying to limit the SELECT * from financial list to the value set in the select field inside my form (HTML), for example, "SELECT * FROM FINANCIAL LIST LIMIT $ search" Doing this:

include("../sqlConnection/connection.php");
$data = json_decode(file_get_contents("php://input"));
$search = $data->fRegistro;
$sql = "SELECT * FROM listafinanceira LIMIT $search";
$stmt = $PDO->prepare( $sql );
$stmt->bindParam(1, $search , PDO::PARAM_INT);
$stmt->execute();

$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);

echo json_encode($result);

but is returning the error stating that fRegistration is not an object.

That is, I am not able to "collect" the value of the select by the value in JSON, if I equate $ search = 10, for example, it works normally. I'm not sure if it was clear enough ... I'm using PDO to disable SQL Injection through prepared statements. The http.get inside the java script to return the query looks like this:

$scope.displayData = function()
    {
        $http.get("../sqlFunctions/selectForm.php",
            {'fRegistro':$scope.fRegistro}

            ).then(function(response){
            $scope.entradas = response.data;
        });
    }

I have tried to change the get method of http request into javascript without success, like this:

$scope.displayData = function()
    {
        $http.get("../sqlFunctions/selectForm.php", {
            params: { 
                'registro': $scope.fRegistro
            }
            }).then(function(response){
            $scope.entradas = response.data;
        });
    }

no html:

<select name="tipo" ng-model="fRegistro" ng-init="fRegistro='10'" ng-class="['uk-select','uk-form-width-small','uk-form-small']">
                    <option value="10">10</option>
                    <option>25</option>
                    <option>50</option>
                    <option>100</option>        
</select>
    
asked by anonymous 22.07.2018 / 22:11

1 answer

0

The problem is that the http get method, by default, does not accept a body and the $http.get of angular does not receive an object with parameters

Send the parameters via url, for example:

$http.get('../sqlFunctions/selectForm.php?registro=${$scope.fRegistro}'}).then( ... );

In php you should also fix some things:

include("../sqlConnection/connection.php");

$search = $_GET["fRegistro"]; //Pega os dados passados por get atrvés da superglobal $_GET

$sql = "SELECT * FROM listafinanceira LIMIT :search"; //Use : e não $

$stmt = $PDO->prepare($sql);
$stmt->bindParam(':search', $search , PDO::PARAM_INT); //Passe o nome que deu acima no SQL, no caso ':search'

$stmt->execute();
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);

echo json_encode($result);
    
22.07.2018 / 22:41