How to work with if and Else?

Asked

Viewed 197 times

1

Good morning.

I would like to get help from you to build this code because I am not able to implement it.

$query1 = "INSERT INTO paciente VALUES ('NULL, $nome_paciente, $nome_doutor')";

$id_paciente = mysql_insert_id();

$query2 = "INSERT INTO servico VALUES ('NULL, tipo_servico, data_servico,{$id_paciente})";

$id_servico = mysql_insert_id();

$query3 = "INSERT INTO estagio VALUES ('NULL, {$id_servico}, data_estagio, tipo_estagio')";

In the following pattern:

if (@mysql_query(INSERT INTO cad_paciente VALUES (NULL, $nome_paciente, $nome_doutor))) {

if(mysql_affected_rows() == 1){
    echo "Registro efetuado com sucesso<br />";
}

}

In this pattern above I can insert only one record in a table as I do to continue inserting.

  • 1

    It was not clear his doubt

  • How to mount this query with the 3 options in the format of the one below checking if it was saved

  • 1

    The simplest way would be to use some driver (Mysqli or PDO) with transaction support, ai vc record the 3 modifications successfully or abort the process on the first failure. In other words, either record everything or not record anything.

  • For example if the servico fail, the Insert of pacientes is reversed or recorded?

  • 1

    That there revert nothing saves and try again

  • 2

    If you don’t know how to work with if and Else, it should be time to study more programming background, understand this as a constructive criticism.

  • @Paulohenriqueneryoliveira its editions are not presenting improvements in the question, is even adding irrelevant things, so it was rejected.

Show 3 more comments

1 answer

-3


From what I understand you should inform what you want to insert where. A good tip to be very clear and organized is using the function sprintf() of PHP.

Try something like this:

<?php

$q = mysql_query(
    sprintf(
        "INSERT INTO cad_paciente (id, nome, doutor) VALUES ('%s', '%s', '%s')",
        "NULL",
        $nome_paciente,
        $nome_doutor
    )
);

if($q) {
    echo "Registro efetuado com sucesso<br />";
}

In this example, the function sprintf() is receiving a string that receives 3 keys, which are the %s inside the code. It takes these keys inside the first string and replaces them with the subsequent parameters that are passed in the function.

I hope that this is your question and that I have answered it.. Abs.

UPDATE

It’s living and learning, rsrs Based on your doubt and using this post as a reference I managed to understand and execute the insertion block once with the mysqli. Follow the code that I think will solve your problem and resolve your doubt ..

<?php

// cria conexão com o banco de dados usando o classe mysqli orientado a objetos
$conn = new mysqli("localhost", "root", "", "test");

$deuCerto = true;

// começa a transação
$conn->query("START TRANSACTION");

if($deuCerto) {
    $query1 = $conn->query("INSERT INTO paciente VALUES (NULL, '".$nome_paciente."', '".$nome_doutor."')");
    $id_paciente = $query1->insert_id;
    // se falhar, para o restante do código
    if(!$query1) {
        $deuCerto = false;
    }
}

// se na insert anterior não tiver sucesso, não executa mais
if($deuCerto) {
    $query2 = $conn->query("INSERT INTO servico VALUES (NULL, '".$tipo_servico."', '".$data_servico."', ".$id_paciente.")");
    $id_servico = $query2->insert_id;
    if(!$query2) {
        $deuCerto = false;
    }
}

// se na insert anterior não tiver sucesso, não executa mais
if($deuCerto) {
    $query3 = "INSERT INTO estagio VALUES (NULL, '".$id_servico."', '".$data_estagio."', '".$tipo_estagio."')";
    $id_estagio = $query3->insert_id;
    if(!$query3) {
        $deuCerto = false;
    }
}

// se nenhuma das inserções derem erro, irá inserir tudo de uma vez
if($deuCerto) {
    $conn->query("COMMIT");
} else { // se houver algum erro, desfaz todos as alterações
    $conn->query("ROLLBACK");
}
  • my doubt and how to continue inserting data in other tables

  • That’s right, thanks for the guidance, now I will only research to understand the functions used here but and that’s just what I wanted. thank you very much

  • Then mark as answer, rsrs

Browser other questions tagged

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