How to update to change a table-specific row

Asked

Viewed 132 times

0

inserir a descrição da imagem aqui

How do I change this table? I’m a beginner in PHP/MYSQL and I’m not sure how to change it. Once the person clicks on the "change" icon, they should be routed to a page, which should pull the id of the specific row clicked on the table. And this is the page where the data changes will be made.

But the point is, it’s two different tables. It has 4 tables. Errors, Customers, Employees and VIEW.

I need to join the table errors with clients, for when making the changes, if I want to change the name of a specific client or an error for ex, it changes the data without problems. Even from different tables.

Follow the Code.

Bank script:

DROP DATABASE IF EXISTS mydb;
CREATE DATABASE IF NOT EXISTS mydb;

USE mydb;

CREATE TABLE clientes (
  cod_clientes INT UNSIGNED NOT NULL AUTO_INCREMENT,
  nome_cliente VARCHAR (50) NOT NULL,
  cnpj VARCHAR (14) NOT NULL,
  PRIMARY KEY (cod_clientes)
) ENGINE = innodb;

CREATE TABLE funcionarios (
  cod_funcionarios INT UNSIGNED NOT NULL AUTO_INCREMENT,
  nome_funcionario VARCHAR (100) NOT NULL,
  login VARCHAR(15) NOT NULL,
  email VARCHAR(80) NOT NULL UNIQUE,
  senha VARCHAR(15) NOT NULL,
  PRIMARY KEY (cod_funcionarios)
  ) ENGINE = innodb;

CREATE TABLE erros(
  cod_erros INT UNSIGNED NOT NULL AUTO_INCREMENT,
  tipo_erro VARCHAR (150) NOT NULL UNIQUE,
  solucao TEXT NOT NULL,
  data_ocorrencia DATE,
  sistema VARCHAR (30) NOT NULL,
  Fk_Funcionarios INT UNSIGNED,
  Fk_Clientes INT UNSIGNED,
  PRIMARY KEY (cod_erros),
  FOREIGN KEY (Fk_Funcionarios) REFERENCES funcionarios (cod_funcionarios) ON UPDATE CASCADE ON DELETE RESTRICT,
  FOREIGN KEY (Fk_Clientes) REFERENCES clientes (cod_clientes) ON UPDATE CASCADE ON DELETE RESTRICT
  ) ENGINE = innodb;

Script by VIEW:

DROP VIEW IF EXISTS VIEW_LISTA_ERROS;

CREATE VIEW VIEW_LISTA_ERROS AS
SELECT clientes.nome_cliente AS Cliente, erros.cod_erros AS ID, erros.tipo_erro AS Erro, erros.solucao AS Solução, erros.data_ocorrencia AS Data, erros.sistema AS Sistema, funcionarios.email AS Email_do_Funcionario
FROM clientes, erros, funcionarios
where erros.Fk_Clientes = clientes.cod_clientes and erros.Fk_Funcionarios = funcionarios.cod_funcionarios
group by cod_erros, Fk_Clientes, Fk_Funcionarios;

select *from VIEW_LISTA_ERROS;

Error log page and customers:

inserir a descrição da imagem aqui

Script where I do Insert:

 //Caso não ocorra nenhum erro, permita que os dados sejam inseridos no banco.
  if ($row == 0) {
    $link = conexao();

    $query = "insert into clientes(nome_cliente, cnpj)
    values('{$nome_cliente}', '{$cnpj}')";
    $result = mysqli_query($link, $query);

    //Recupero o id do cliente que acabou de ser inserido
    $idCliente = mysqli_insert_id($link);

      //Recupero o id do funcionaio que está logado na sessão
    $idFuncionario = $_SESSION["id_usuario"];

    $query2 = "insert into erros(tipo_erro,solucao,data_ocorrencia,sistema, Fk_Funcionarios, Fk_Clientes)
    values('{$nome_erro}', '{$solucao}', '{$data}', '{$sistema}', '{$idFuncionario}', '{$idCliente}')";
    $result2 = mysqli_query($link, $query2);

  header('location: paginaConsulta.php');
  }

The part where I pull the Vieww created to list the specific data in the table:

         <?php
            $link = conexao();
            $query = $link->prepare('select Cliente, Erro, Solução, Data, Sistema from VIEW_LISTA_ERROS');
            $query->execute();
            $query->bind_result($cliente, $erro, $solucao, $data, $sistema);

            echo "
            <!--Cabeçalho-->
            <div class='row header'>
                <div class='cell'>
                CLIENTE
                </div>
                <div class='cell'>
                ERRO
                </div>
                <div class='cell'>
                SOLUÇÃO
                </div>
                <div class='cell'>
                DATA
                </div>
                <div class='cell'>
                SISTEMA
                </div>
                <div class='cell'>
                <i class='fas fa-tools'></i>
                </div>
            </div>
            ";

            while ($query->fetch()) {
                echo "
                <div class='row'>
                    <div class='cell cell2' data-title='Clientes'>
                        $cliente
                    </div>
                    <div class='cell cell2' data-title='Erro'>
                        $erro
                    </div>";

                   echo "
                     <div class='cell cell2' data-title=Solução style='vertical-align:middle; text-align: justify;'> ";
                        echo nl2br("$solucao");
                    echo "</div>";

                    echo"
                    <div class='cell cell2' data-title='Data'>";
                        $data = formatarData($data);
                    echo "$data";   
                    echo"
                    </div>
                    <div class='cell cell2' data-title='Sistema'>
                        $sistema 
                    </div>
                    <div class='cell cell2' data-title='Sistema'>
                        <a href=''><i class='fas fa-edit'></i></a>
                    </div>
                </div>
            ";
            }
            echo "  
            <!--Final Conteúdo-->
        </div>
    </div>
</div>

";

Can someone please assist me!? Give me a hint or a logic.. How can I solve this??

  • You can locate the user via cod_clients and give an UPDATE on it

  • Can someone teach me how to update in this case??

  • dx already solved the problem

No answers

Browser other questions tagged

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