Insert into and select from php

hello i have 2 tables account_info and patient_info i want the user to fill first the account_info table and then the patient_info
but i have to select account_id and patient_username from account_info and insert them in patient_info then the user enters patient name and the rest of the information
i have :

  $sql  = "INSERT INTO patient_info(p_username, acc_id, p_fname, p_lname, p_gender, p_condition, 
 p_birthdate, p_emergencycontact) 
					SELECT p_username, acc_id
						FROM account_info      
  VALUES(:p_fname, :p_lname, :p_gender, :p_condition, :p_birthdate, 
:p_emergencycontact)";

Your database design is incorrect. Rather than tell us how you are attempting to handle the task at hand, tell us about the project and what it should do. (Not how your trying to do it).

It would appear to be some sort of patient registration. Tell us details of that. Can the account person be different from the patient or are they they same person. Provide the big picture of what is going on.

Of course! I am creating an app where i have 2 users admin and patient so each account has 2 users .
account_info(acc_id(pk, auto inc), p_username, a_username,password)
These will be entered on one page.
After that the user goes to enter the patient information. But i don’t want the user to enter p_username again. I want to insert them in the table and then he can insert the rest of the information.
patient_info ( p_username (pk), acc_id, p_fname,…)
The admin_info table has amost the same table as patient_info
The commin link is acc_id.
I hope i made it clear enough! thank you

But, presuming you’re talking about two HTML pages, first one has user entering account info then submitting the form, and the second processes that data, inserts it into the database and then draws the form for them to enter the patient info, then the second page already has the account id, either because the user entered it on the first page, or (as it’s an auto-increment column) from the last-insert-id after the query has run.

Pseudo-code:

first-page: HTML form, user presses submit
second-page:
  form variables checked, if OK, query run to insert account information
  last-insert-id grabbed from query result
  form for patient info rendered, acc-id stored in form as hidden value.
third-page:
  form vars from second page form checked
  if OK, query run to insert patient_info.

i understand what you’re saying but i don’t know how to write it. I’m sorry but I am new to this i unerstand the logic but i don’t know how to write it

I’m creating an application where I have 2 users. An admin and patient so each account has 2 users.( where the admin adds medication and other to do list for the patient to do).

Both admin and patient can log in to the same account this is why acc_id is the common link between them.

account_info(acc_id(pk, auto inc), p_username, a_username,password)

These will be entered on one page.

After that the user goes to enter the patient information. where he enters Patient information.

patient_info ( p_username (pk), acc_id, p_fname,....) 

The admin_info table has almost the same table as patient_info

How do i write the insert code in a way where acc_id and p_username are selected from account_info table and the rest of the information is just inserted into the table patient_info.

My code so far:

 $sql  = "INSERT INTO patient_info(p_username, acc_id, p_fname, p_lname, 
p_gender, p_condition, 
p_birthdate, p_emergencycontact) 
				SELECT p_username, acc_id
					FROM account_info      
 VALUES(:p_fname, :p_lname, :p_gender, :p_condition, :p_birthdate, 
:p_emergencycontact)";

I am new to php please don’t downvote I understand the logic but I don’t know how to write it

Since you don’t know how to write it are you looking to hire someone to do it? We will help with your attempt but generally do not write the code for you.

Is this for a real business use? If so, there is a higher level of design that needs to go into this. You are asking for answers on how to accomplish your incorrect approach.

There are plenty of Medical database schema’s you can look up that will help you get the DB design straightened out. When you find yourself repeating information that is a big red flag that the DB design is wrong.

Check out Open MRS (Medical Record System) http://openmrs.org/demo/ and the DB design behind it.

This open source application may even be a solution for your needs.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.