hii everyone...
i am working on a webpage as my class assgnmnt nd using php-mysql for that..
plz guide me how this thing can be done?? i want to add some thing in a table and if there is a table of same fields(columns) that i want then add in that table otherwise create an another table of those fields and add data in it...

Dani AI

Generated

asked for logic that inserts into an existing table when its columns match, or creates a new table otherwise. and pointed to basic create-and-insert and form workflows, but those do not verify column compatibility. A reliable approach is to read the server metadata, compare the existing table definition to the expected columns, then decide to insert, alter, or create.

Query INFORMATION_SCHEMA.COLUMNS to get the current column list and types, then compare that result in your application to the expected schema. Example queries:

SELECT COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'your_db' AND TABLE_NAME = 'your_table'
ORDER BY ORDINAL_POSITION;
SELECT COUNT(*) AS matched
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'your_db' AND TABLE_NAME = 'your_table'
  AND COLUMN_NAME IN ('col1','col2','col3');

If the matched count equals the number of expected columns, inspect COLUMN_TYPE/DATA_TYPE for type compatibility before inserting. If columns are missing, choose one of: ALTER the table to add missing columns (careful with existing data), or create a new versioned table and insert there. In concurrent environments, handle the race where two processes try to create the same table—catch the duplicate-table error and re-check metadata. Always validate and whitelist any dynamic table or column names (never interpolate raw user input), use parameterized queries, and ensure the DB user has appropriate privileges.

For long-term maintainability, consider using a single table with a dataset identifier or a JSON column instead of creating many dynamic tables. See the MySQL INFORMATION_SCHEMA documentation for details: MySQL INFORMATION_SCHEMA.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

Have you checked the online MySQL manual?

"CREATE TABLE tbl_name IF NOT EXISTS"

then run your insert query

probably you just need to code a simple script for registration which will insert into the mysql table using above query.
Just create few html elements inside a form.
and once you submit the form using submit button, process those values using $_POST and your query there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.