Consult the database when WHERE is what I send from the Angularjs driver

0

I have a conection.php that connects to the DB and queryTickets.php :

<?php  
header('Access-Control-Allow-Origin: *');  
date_default_timezone_set("Chile/Continental");

**// Including database connections**  
require_once 'conection.php';

**// mysqli query to fetch all data from database**    
$query = "SELECT rut, numero FROM Tickets WHERE servicio_id = $_POST['servicio_id']";  
$result = mysqli_query($con, $query);  
$arr = array();  

if(mysqli_num_rows($result) != 0) {  
while($row = mysqli_fetch_assoc($result)) {  
$arr[] = $row;  
}  
}  
**// Return json array containing data from the databasecon**  
echo $json_info = json_encode($arr);  
?>

I have the problem in Query in WHERE because I want to send the parameter from the AngularJS driver because of the data I get from a login ... (another WS). p>

The question is: how can I do it?

In angularJS it should be as follows:

$http({
    method: 'POST',
    url: 'http://localhost/Paneldetencion/app/php/consultaTickets.php',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
    data: {
        servicio_id: 38
    }
})

.then(function(data) {


var dat = data.data;
$scope.tickets = [];

      for (var i = 0; i < dat.length; i++) {
        dat[i]
        var ticket = {
            numero: dat[i].numero,
            rut: dat[i].rut,

        };
        $scope.tickets.push(ticket);
    } 

}

Is the number an integer, how should it be parsed?

error: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /Applications/XAMPP/xamppfiles/....php on line 7

    
asked by Hernan Humaña 03.11.2016 в 15:27
source

2 answers

1

After trying I arrive at the following solution:

<?php
    $db = mysqli_connect('localhost','root','','prueba')
    or die('Error connecting to MySQL server.');
    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $id = $request->servicio_id;
    $query = "SELECT * FROM servicios WHERE id=".$id;
    mysqli_query($db, $query) or die('Error querying database.');
    $result = mysqli_query($db, $query);
    $array = array();
    while ($row = mysqli_fetch_array($result)) {
        $array[] = array(
            "id" => $row["id"],
            "sucursal_id" => $row["sucursal_id"],
            "nombre" => $row["nombre"],
            "nombre_completo" => $row["nombre_completo"]
        ); 
    }
    echo json_encode($array);
?>

Modify the code of the connection to the database and probe in my xampp with php 7.0 and the ones you occupy are obsolete in that version.

Change the query to

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$id = $request->servicio_id;
$query = "SELECT * FROM servicios WHERE id=".$id;

When sending from your AngularJS you are doing a POST

headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
},

With these headers you are therefore predicting that the data sent is of JSON and PHP is not receiving a $_POST accordingly.

That's why when we tried to print $_POST['servicio_id'] we always received an index undefined

    
answered by 03.11.2016 / 22:56
source
3

You can not see that the problem is AngularJS but how you capture the parameters from PHP .

I imagine something like:

$servicioId = $_POST["servicioId"];

should serve you assuming that the parameter you send is named servicioId .

But that's not going to be enough since you must also link that value to be included in the query.

This link can help you.

    
answered by 03.11.2016 в 16:19