How to do 2 Insert at the same time with PHP in MYSQL Database?

Asked

Viewed 375 times

0

Database:

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 VARCHAR (20) NOT NULL,
  PRIMARY KEY (cod_clientes)
) ENGINE = innodb;

CREATE TABLE erros(
  cod_erros INT UNSIGNED NOT NULL AUTO_INCREMENT,
  nome_erro VARCHAR (150) NOT NULL,
  solucao VARCHAR (255) NOT NULL,
  data_ocorrencia DATE,
  sistema VARCHAR (30) NOT NULL,
  PRIMARY KEY (cod_erros)
  ) ENGINE = innodb;

CREATE TABLE funcionarios (
  cod_funcionarios INT UNSIGNED NOT NULL AUTO_INCREMENT,
  login VARCHAR(15) NOT NULL,
  email VARCHAR(50) NOT NULL,
  senha VARCHAR(15) NOT NULL,
  cpf VARCHAR (11) NOT NULL UNIQUE,
  PRIMARY KEY (cod_funcionarios),
  Fk_Clientes INT UNSIGNED,
  Fk_Erros INT UNSIGNED,
  FOREIGN KEY Fk_Clientes (Fk_Clientes) REFERENCES clientes (cod_clientes) ON UPDATE CASCADE ON DELETE RESTRICT,
  FOREIGN KEY Fk_Erros (Fk_erros) REFERENCES erros (cod_erros) ON UPDATE CASCADE ON DELETE RESTRICT
  ) ENGINE = innodb;

View:

CREATE VIEW VIEW_LISTA_ERROS AS SELECT nome AS Cliente, 
nome_erro AS Erro, solucao AS Solucao, 
data_ocorrencia AS Data, sistema AS Sistema 
FROM  erros, clientes;

select *from VIEW_LISTA_ERROS;

Part of PHP code where I’m trying to insert:

if ($row == 0) {
    $query = "insert into erros(nome_erro,solucao,data_ocorrencia,sistema)
    values('{$nome_erro}', ('{$solucao}'), ('{$data}'), '{$sistema}')";

    $query2 = "insert into clientes(nome)
    values('{$nome_cliente}'";

    mysqli_query(conexao(), $query);

    mysqli_query(conexao(), $query2);

    header('location: paginaConsulta.php');
  }
}
  • the answer helped you? If yes, try to schedule it to help other colleagues.

1 answer

0


Good afternoon, change the order of execution like this:

if ($row == 0) {
    $query = "insert into erros(nome_erro,solucao,data_ocorrencia,sistema)
    values('{$nome_erro}', ('{$solucao}'), ('{$data}'), '{$sistema}')";
    mysqli_query(conexao(), $query);

    $query2 = "insert into clientes(nome)
    values('{$nome_cliente}'";
    mysqli_query(conexao(), $query2);

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

see if it helps you

  • He is not entering in the "clients" table. And when I pull up the view, it brings in all the other data that I’ve logged, except for the new client who’s registered. That is, it is only registering the table errors data. I believe I would have to do a process for this, but n know how it does in php, etc.

Browser other questions tagged

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