Request for PHP service

Asked

Viewed 121 times

1

I am doing a Client/Server exercise, I have an sql with multiple product records and I can LIST ALL, FILTER BY PRICE and FILTER BY CODE, but when implementing the service of changing stock, it does not recognize my fuction. The part 1 I managed to accomplish, from the 3rd topic of part 2 I could not ...I will put the statement and the codes! Thanks who help

EXERCISE

Part 1: Installation

  • Clone the files from this folder on your machine
  • Import the base.sql file into Mysql using the tool of your preference
  • Put the Catalog.php file in your Apache httdos directory machine
  • Test the connection by accessing the php file on your computer, indicating the file path. Example: http://localhost/Catalog.php? op=list
  • Test other implemented services

Part 2: Creation of the client application

  • Create a client application using the concepts shown in the lessons previous.

  • 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 manner user clear. Note that the Catalog.php service returns the data in json format

  • Your job will be to convert that answer into something more readable for non-technical users (example: using table to display data)

Part 3: Creation of new services

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

  • Create new services that can be run by client applications

  • Some examples: insert product, delete product, increase stock of a product, readjusting the 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);
}
?>
  • 1

    The statement is only text, there is no reason to put here as image. Please transfer the statement as text; this will help in the search of the site and will allow users who cannot view the image to help you. Taking advantage, nay use the site snippet for PHP; it is quite clear in it that it is for HTML/Javascript/CSS code. When it is [Edit] the question, put also the error messages. We have no way to guess this.

  • What is the error that appears?

  • He’s not even acknowledging my Seven Story... I don’t know much about PHP

  • $_GET['op'] is exactly "setEstoque"? Because if it is "SetEstoque" won’t work, the comparison is case sensitive.

  • She looks exactly like setEstoque

No answers

Browser other questions tagged

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