Mysql insertion problem

Asked

Viewed 51 times

0

I have a form Scheduling and one of Employee Register, one script to enter data in the database table Scheduling and another script for the Register. In the Scheduling I need to rescue the code Employee who is doing the scheduling.

My question is how I salvage the primary key value from the employee table and put it into the schedule table. Considering I already passed the foreign keys.

<?php
        require_once "conexao01.php";

        function filtrarDados($dado) {
            $dado = trim($dado);
            $dado = stripslashes($dado);
            $dado = htmlspecialchars($dado);

            return $dado;
        }


        if ($_SERVER["REQUEST_METHOD"] == "POST"){

            $connecting = conectaAoMySql();

            $especialdade = $medico = $data = $horario = $nome = $telefone = "";

            $nome =             filtrarDados($_POST["NomePaciente"]);
            $telefone =         filtrarDados($_POST["TelPaciente"]);
            $especialdade =     filtrarDados($_POST["especialidades"]);
            $medico =           filtrarDados($_POST["medico"]);
            $data =             $_POST["dataConsu"];
            $horario =          $_POST["horaConsult"];

            // Tentativa de resgatar o codFuncionario
            $sql = "
                SELECT codFuncionario FROM tblFuncionario WHERE EspecialidadeMedica=$especialdade
            ";
                //Recebe o que o $sql resgato
               $resultado = $connecting->query($sql);

            try {
                //Inicio da transação
                $connecting->begin_transaction();

                $connecting->autocommit(FALSE);

                // Inserção de Dados
                $dados=$connecting->query(" insert into tblpaciente (codPaciente, Nome, Telefone) values (null, '$nome', $telefone)");

                $dados2=$connecting->query(" insert into agenda (codAgendamento, Especialidade, Medico, DataConsulta, Horario, codId, codFuncionario) values (null, '$especialdade', '$medico', '$data', '$horario', LAST_INSERT_ID(), '$resultado')");

                $connecting->commit();
                echo "Executado com Sucesso!";
                $connecting->close();
            }

            catch (Expection $e) {
                    $connecting->rollback();

                    echo "Ocorreu um erro na transação: " . $e->getMessage();
            }
        }

    ?>

-

try
    {

        $conn->begin_transaction();

        $conn->autocommit(FALSE);

        $dados=$conn->query("insert into tblfuncionario (codFuncionario, NomeFuncionario, DataNascimento, Sexo, EstadoCivil, Cargo, EspecialidadeMedica, CPF, RG, Outro)
            values (null, '$nomeFunc', '$dataNasc', '$sexoFunc', '$estadoCivil', '$cargo', '$especialidade', '$cpf', '$rg', '$outro')");

        $dados2=$conn->query("insert into enderecofuncionario (CEP, Logradouro, Numero, Complemento, Bairro, Cidade, Tipo, UF, codId)
        values ('$cep', '$logradouro', '$numero', '$complemento', '$bairro', '$cidade', '$tipo', '$uf', LAST_INSERT_ID())");

        $conn->commit();

        echo "Transacao efetuada com sucesso";
        $conn->close();
    }

    catch (Exception $e)
    {
        $conn->rollback();

        echo "Erro na transacao: ". $e->getMessage();
    }
}
  • 1

    If the employee you are registering with is logged in to the system, just take their session data, COOKIE or SESSION, depending on how you have them authenticate.

  • Put your insertion script so we can assist you.

  • @W.Faustino, but if the employee is a doctor sometimes he may not be logged in, but in the Scheduling table, there has to be the code of the doctor who will consult.

  • But the value of the doctor goes in the column 'Doctor' and the employee goes in the column 'codFunctioning', the latter must always be logged in, vc feeds the session ID, and the value of 'Doctor' vc feed the value filled in the form.

  • Medico Faustino codMedic (same thing codFunctioning) = 1&#Xa Faustino and automatically in the scheduling table appear the doctor’s code instead of the name.

  • I’m not sure if LAST_INSERT_ID() will work as you wish. Give a look in that reply in particular on $id = $mysqli->insert_id;

  • @rray the question is that they are different scripts. A way q imagined that would work, would be to make a select but n is giving.

Show 2 more comments
No answers

Browser other questions tagged

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