Inserting data in PHP

Asked

Viewed 245 times

1

Good morning! Staff need a Help I am facing the following problem

  • I have two tables in the database (Clinics) and (users)

  • I want to insert the data into the two tables in a sensitive way, so
    that is done sending the form, the form data i already
    I’m pulling from the form all the data are already being picked up.

  • the problem and that I need to enter the fields of the same form in
    individual tables

  • on the table (Clinics) I just have a field called clinic_name where,
    this shall be the first field to be filled in at the time of dispatch
    form because in this table (Clinics) an id will be generated
    I have to pick to fill the table (users) in sequence

  • where to fill the table (users) consequently I will have what to do a search in the table (Clinics) and get the clinic id and
    fill in the field user_clincis_id in the table (users)

  • The two tables are (Clinics) and (users) key relationship
    among them where foreign (Clinics 1) (users n) one for
    many associative

  • Well I’ve done a lot of things I’ve even thought about doing a routine random

  • more I want an opinion of vcs which would be the best way to follow the images below, the image routine below so far only fill the table users

Relacionamento entre tabelas modelagem

inserir a descrição da imagem aqui

2 answers

2


Opa all right? Considering it is a PHPOO I can give this example. In SQL you cannot do in a query the Insert in two different tables I suggest at the end of the method of saving the clinic use a

 return $variavelConexaoBD->lastInsertId();

Already in your so-called save method

$id_clinica = $EntidadeClinica->salvar_clinica($objClinica); 

After saving the clinic it will return the ID by the PHP method lastInsertId()

$EntidadeUsers->salvar_usuarios($objClinica, $id_clinica);
  • 2

    Gustavo I’ll try this routine here I’ll adjust it to my routine here and see if it goes well, finish here I come back to tell you if it worked, Thanks

  • 2

    To be able to solve this way I created the method to get the last id in the parent class and in the daughter class I called the method to get id under the name of the clinic I called the method to get the last id inserted and then I continued inserting the data already with id of the clinic worked beauty, I used the lastInsertId() method that you mentioned worth

  • Okay, thanks for the feedback, I’m also a bit of a beginner in php but my poblema was similar to yours. Hug.

0

Looks like you’re using codeigniter, so to get the id inserted you can use

$this->db->insert_id

This way you can add in the Clinics table and then grab the inserted id to add in the users.

Source

Browser other questions tagged

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