Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this questionIs it possible to create a variable influenced table?
Like instead of creating a standard table. Can you let users create a custom table. Like on sign up can I do a CREATE TABLE
under a database and the table's name is influenced by a PHP variable? And the same thing for the fields in the table?
For example, $username.table
for tables and $username_field
for fields in the table?
Why? I need user to be able to add fields they need. Sort of like letting them create an onlin form
Then you need to engineer your database to allow for this. Not be lazy and create tables for form fields. Not only is this a security risk, it will never scale well. Here is a really poor example of how to engineer a table based system to handle dynamic form generation.
DROP TABLE "main"."form";
CREATE TABLE "form" (
"id" INTEGER NOT NULL,
"name" TEXT NOT NULL,
PRIMARY KEY ("id")
);
DROP TABLE "main"."form_field";
CREATE TABLE "form_field" (
"form" INTEGER NOT NULL,
"id" INTEGER NOT NULL,
"name" TEXT NOT NULL,
PRIMARY KEY ("form", "id")
);
Hopefully your imagination can take you from here.
It's certainly possible, you just need to write well formed CREATE TABLE
/ALTER TABLE
queries.
To give your users the ability to create custom fields does not necessitate custom/dynamically created tables.
Consider instead creating a Custom_Fields
table that can hold user created fields as rows.
e.g.
User Table
| userid | username |
Custom Field Table
| fieldid | userid | field_name | field_value
Get My Custom Fields/Values:
SELECT `field_name`,`field_value` FROM `custom_fields` WHERE `userid`=$myUserId
As an option you can create SQLite database for each user and place it in user assigned directory.
This is not how you're supposed to use databases. If you set up your database like this, you will not be able to make use of all (most) functions your query language.
But to create tables (in mysql what I assume you're using), make use of the CREATE TABLE query.
精彩评论