Add Fields to an Existing MySQL Table |
8/3/2024 11:22:55 AM |
PHPRunner General questions | |
A
alfonso authorDevClub member
I would like to know if it is possible to create a form in which fields are added to an existing table. And then, from another section, to be able to add records to said table with the created fields. |
|
C
|
cristi 8/4/2024 |
You can do that with SQL but it is uncommon: for example provided that all clients have an primary key - client.id - you can create a new table let's say client_preferences and store in ENUM fields all these custom fields and retrive them later based on client.id - I hope this is enough to get you started. You also need to create another table for client_attributes that will tell you what kind of field the client added based on the client.id. There is also the "ugly" solution of adding a lot of custom fields to the table and made them available to users per request and run a query to insert those custom fields - not that elegant and modern. Here is an article for you: article An anther one: article |
![]() |
Sergey Kornilov admin 8/4/2024 |
Adding a column to the database table is not difficult but not very useful. You simply need to execute a SQL query like this:
But the big question is - what are you going to do with this new field. PHPRunner is not aware of this new column and cannot do anything about it. If you need to work with database structure that can change, you need to take a different approach. Check Forms template for instance, that does something very similar: |
M
|
MikeT 8/4/2024 |
I would avoid this as much as possible, but if there's the need for a dynamic structure it is usually done with a field that stores JSON. Xlinesoft does this e.g. in the documents manager template to store meta data about files. You can then parse out the relevant data in "view as". |
C
|
Chris Whitehead 8/4/2024 |
As it's been said, JSON is ideal for this, if your DB doesn't support JSON, what about using another table for the extra fields and access them as a child table to your main clients table which you can display on the edit page. I have an older DB so I can't use JSON on that one , I also have different types of users, some need extra fields and others don't, I tried to explain it in English but it sounded like I was just waffling, so I ran my ramblings through chatGPT so it's sounds a bit more sensible. Problem Solution client_details Table CREATE TABLE `client_details` ( client_id: Links to the main client record. CREATE TABLE `client_detail_fields` ( details: Name of the extra field. How It Works |
M
|
MikeT 8/4/2024 |
Just as a clarification: the db engine itself doesn't necessarily need to support JSON natively, you can just use a text fields to store the JSON blob and add code in the field display / store events to parse/encode back and forth. As said, an example can be found in the documents manager template. |
C
|
Chris Whitehead 8/5/2024 |
@MikeT You're right, it doesn't need to support the JSON search functions, you can just json_encode it and drop it in. I was thinking that @alfonso wanted to add a field and then make it available in the other clients. |
A
|
alfonso authorDevClub member 8/5/2024 |
Thanks for the clarifications. |
C
|
cristi 8/5/2024 |
What you want is not something trivial than can be shown in a couple of lines of code... |
![]() |
mbintex 8/5/2024 |
One should not forget that nowadays our products - if we offer our software as Saas - will be compared to solutions like Airtable, Smartsuite and so forth. And there users can add and alter fields to there likings. This is getting a "standard" in my eyes and not something too sophisticated. So a decent update to v11 could bring the ability for more flexibility in this respect. Same with the ability to online change views and print layouts to the likings of the user. |
M
|
MikeT 8/6/2024 |
expanding on @mbintex remarks and following the discussion here: What I personally miss the most is a concept of "unbound fields", maybe people with experience in other RAD tools like Access know what I mean. |