Save data to various tables

Asked

Viewed 250 times

1

I am making a registration system and would like a help from you to be able to save the data in several tables.

My registration is composed by the following fields.

Nome do Paciente
Nome do Doutor
Tipo de Serviço
Data de Entrada
Estagio

Now so is my database table:

id_paciente nome_do_paciente nome_do_doutor

This data will be saved in the patient table and accurate the id of this record to give sequence.

id_servico tipo_servico data_servico id_paciente

This data will be saved in the service table and I also need the id of this record.

id_estagio id_servico data_estagio tipo_estagio

This data will be saved in the stage table and also need the id of this record.

Abre_Conexao();

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

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

}

I’ve seen it working this way below but I want to add to this pattern above because my code is all in it

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

$id_paciente = mysql_insert_id();

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

$id_servico = mysql_insert_id();

$query3 = "INSERT INTO estagio VALUES ('NULL, {$id_servico}, data_estagio, tipo_estagio')";
mysql_query( $query3 );
  • And php goes where in this story?

  • I edited the question informing better what I need

2 answers

1

I think PHP has a native function (mysql_insert_id) to return the generated ID.

Try to take a look at this documentation: http://php.net/manual/en/function.mysql-insert-id.php

ex.

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

if (!$link) {

die('Could not connect: ' . mysql_error());

}

mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>

0

do so:

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

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

}

however, after studying a little more PHP and Mysql and having everything working, look for another method to insert the data or at least validate the data before doing things like this, because this method allows a type of attack called SQL Injection.

  • Nelson I want to continue saving the records so that I make only a registration screen and not multiple screens each for each table, I want to make only a screen and enter in it all the data and when sending to do the Insert it already distribute the data automatically

  • $Query1 = "INSERT INTO test (value) VALUES ('test')"; mysql_query( $Query1 ); $id = mysql_insert_id(); $query2 = "INSERT INTO test_2 (test_id, value) VALUES ({$id}, 'test')"; mysql_query( $query2 );

  • Like the one I posted in the commentary

  • 1

    @Cristianocardososilva looks... the best is to elaborate this on another question. Pq so in the comment was very confused. In the answers I showed you how to insert data into a table. Now, the following logic is up to you.

  • Have to check the question again

  • and what’s the problem ? use if(mysql_affected_rows() for each query

  • I couldn’t put it together, he only saves the first

  • remove the simple quotes and try again

Show 3 more comments

Browser other questions tagged

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