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");
}
It was not clear his doubt
– rray
How to mount this query with the 3 options in the format of the one below checking if it was saved
– Cristiano Cardoso Silva
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.
– rray
For example if the
servico
fail, the Insert ofpacientes
is reversed or recorded?– rray
How to rescue ID from last record recorded with Mysqli and Write data to two tables from a PHP form
– rray
That there revert nothing saves and try again
– Cristiano Cardoso Silva
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.
– Guilherme Guini
@Paulohenriqueneryoliveira its editions are not presenting improvements in the question, is even adding irrelevant things, so it was rejected.
– user28595