0

it the prespcription table,here you see medicine_id column can not get 3 inputs from dropdown menushere select the dropdown value

I am a beginner of CodeIgniter. I have some issues in arrays. I have 3 dropdown menus to select a medicine that is fetched through the medicine database. but I don't understand how to put 3 values in one field of the table. there are two tables:

  1. Medicine table(medicine_id ,medicine_name)
  2. Prescription table(prescription_id,patient_id,symptoms,disgonis,medicine_id,m_note,date)

How to put multiple values in a medicine_id column?

Cœur
  • 37,241
  • 25
  • 195
  • 267
AYAN ADHIKARY
  • 33
  • 1
  • 4
  • 12

3 Answers3

2

you cant put multiple values into one column in mysql. You got a so called 1:n relation here. 1 Prescription can have n medicines.

For representing that relation in mysql you need to create a new table

Prescription_Medicine(prescription_id,medicine_id)

where you have one row for every medicine thats on the prescription. You should get a quick overview over database normalization as the commenters suggested.

Sam Tigle
  • 393
  • 3
  • 14
2

First Approach: Not Recommended

You can set the medicine_id field as varchar and save all medicine id in comma separated form. When you need to join Prescription table to Medicine table you have to user FIND_IN_SET. But it is not recommended because it is against normalization rules. and it will slow down your execution.

Second Approach:

You can make another table, where you will store prescription_id and medicine_id. As @Sam Tigle told you.

Alok Mali
  • 2,821
  • 2
  • 16
  • 32
1

You can save an array into a database field in different ways

  1. Serialized form
  2. Comma-Separated string
  3. JSON (Read more about JSON data type here)

But it is not the proper solution for your problem. You should consider a workaround as given below:

Usually, a single prescription can have multiple medicines. Therefore a One to Many relation is there.

It is recommended that you create two tables as prescriptions and prescription_medicines, and save the prescription details in the prescriptions table and then add each medicine in a prescription as a row in the prescription_medicines table.

The table structure can be something as shown below.

prescriptions table

id (primary_key), doctor_id, patient_i`, date, .....

prescription_medicines table

id, prescription_id, medicine_id, dosage, ...

Here the prescription_id is the primary_key of prescriptions_table and the medicine_id should be a primary_key of medicines_table

Now we can relate the prescriptions with medicines table.

EXAMPLE QUERY: To get the prescribed medicines and its dosage for a patient having patient id 123

SELECT med.name, pres_med.dosage 
FROM prescriptions AS pres
JOIN prescription_medicines AS pres_med ON pres_med.prescription_id = pres.id
JOIN medicines AS med ON med.id = pres_med.medicine_id
WHERE pres.patient_id = 123;
-- ADD YOUR OWN LOGIC TO GET THE LASTEST/RELEVANT PRESCRIPTION
ash__939
  • 1,614
  • 2
  • 12
  • 21