Service request in PHP

1

I am doing a Client / Server exercise, I have a sql with several product records and I can LIST ALL, FILTER BY PRICE and FILTER BY CODE, but at the time of implementing the stock change service, it does not recognize my fuction. Part 1 I was able to accomplish, from the 3rd topic of part 2 I could not ... I will put the statement and the codes! Thanks for the help

  • Clone the files of this folder on your machine
  • Import the base.sql file into MySQL using the tool of your choice
  • Place the catalog.php file in the Apache https directory of your machine
  • Test the connection by accessing the php file on your computer, indicating the file path. Example: link
  • Test other implemented services

Part 2: Creating the client application

  • Create a client application, using the concepts shown in the classes above.

  • Your client application must access the service deployed in part 1, passing the values chosen by the user

  • Implement response processing, showing the data in a way clear to the user. Note that the catalog.php service returns the data in JSON format

  • Your job will be to convert this response into something more readable to non-technical users (example: using table to show data)

Part 3: Creating new services

  • In the catalog.php file, 3 services have been implemented

  • Create new services that can be run by client applications

  • Some examples: insert product, exclude product, increase inventory of a product, readjust price of one or more products

<?php
$mysqli = new mysqli('localhost','root','root','aula3_clientserver');
$op = $_GET["op"];
switch ($op) {
    case "list":
        listar($mysqli);
        break;
    case "filterCode":
        getByCode($mysqli, $_GET["code"]);
        break;
    case "filterPrice":
        getByPrice($mysqli, $_GET["from"],$_GET["to"]);
        break;
    case "updateEstoque":
        setEstoque($mysqli, $_GET["code"],$_GET["qtd"]);
        break;
    default: 
        echo "Operacao invalida";
        break;
}
function listar($mysqli) {
    $query = "SELECT produto_id, nome, estoque, preco from produto";
    $result = $mysqli->query($query);

    $dbdata = array();
    while( $row = $result->fetch_assoc()) {
        $dbdata[]=$row;
    }
    echo json_encode($dbdata);
}
function getByCode($mysqli, $code) {
    $query = "SELECT produto_id, nome, estoque, preco from produto where nome='".$code."'";
    $result = $mysqli->query($query);
    $dbdata = array();

    while( $row = $result->fetch_assoc()) {
        $dbdata[]=$row;
    }
    echo json_encode($dbdata);
}
function getByPrice($mysqli, $from, $to) {
    $query = "SELECT produto_id, nome, estoque, preco from produto where preco>=".$from." AND preco<=".$to;
    $result = $mysqli->query($query);
    $dbdata = array();

    while( $row = $result->fetch_assoc()) {
        $dbdata[]=$row;
    }
    echo json_encode($dbdata);
}
//AUMENTAR ESTOQUE
function setEstoque($mysqli, $code, $qtd) {
    $query = "UPDATE produto SET estoque = estoque +".$qtd." WHERE nome ='".$code."'";
    $result = $mysqli->query($query);
}
?>
    
asked by anonymous 13.09.2018 / 19:44

0 answers