hey all i got a question where i am not sure that it will be possible.

i already have my table in my data base which has values in it ex: first_property. and i made a little script which helps me grab all the names in the form that i will be posting to the DB and puts it into a sql query.
ex:
ALTER TABLE `properties` ADD `first_property` TEXT NULL ,
ADD `second_property` TEXT NULL ,
etc
;

but i do have duplicates on different pages and i run into the error
#1060 - Duplicate column name 'first_property'

now the question: is there a syntax for sql to skip that name if it exist

thanks for any help.

Dani AI

Generated

Short answer: either use the server-side "IF NOT EXISTS" support (if your MySQL/MariaDB version has it) or detect existing columns before issuing ALTER. 's Transact‑SQL example won't help here because phpMyAdmin talks to MySQL, not SQL Server, and 's prompt about why you add fields is relevant — dynamically adding lots of columns is brittle and worth rethinking.

If the server supports it, the cleanest fix is an ALTER that skips existing names. Check your server version and the manual first; examples and syntax are in the MySQL docs:
ALTER TABLE — MySQL Manual

If you cannot rely on that syntax, query INFORMATION_SCHEMA to see which form fields map to already‑present columns and only ALTER for the missing ones. Example check (run once per candidate column):

SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'your_database'
  AND TABLE_NAME = 'properties'
  AND COLUMN_NAME = 'first_property'
LIMIT 1;

A practical approach when using php/phpMyAdmin:

  • Build an associative list of form fields you want to add.
  • For each name, query INFORMATION_SCHEMA (or cache a single list of existing columns) and skip names that already exist.
  • Issue one ALTER TABLE with only the new columns, or loop and add each missing column.

Sample PDO workflow (conceptual): fetch existing column names, array_diff against submitted names, then ALTER with only the missing columns.

A design note for : if you frequently add arbitrary property names, consider an attribute/value table instead of altering the schema for every form. Storing properties as rows (entity_id, property_name, value) avoids schema churn and scales better for lots of dynamic fields — see the Entity‑Attribute‑Value pattern for tradeoffs: Entity–attribute–value model.

If immediate help is needed, post the MySQL version (SELECT VERSION()) and a sample of your form-to-column mapping so a concrete script can be suggested.

Recommended Answers

All 4 Replies

hey all i got a question where i am not sure that it will be possible.

i already have my table in my data base which has values in it ex: first_property. and i made a little script which helps me grab all the names in the form that i will be posting to the DB and puts it into a sql query.
ex:
ALTER TABLE `properties` ADD `first_property` TEXT NULL ,
ADD `second_property` TEXT NULL ,
etc
;

but i do have duplicates on different pages and i run into the error
#1060 - Duplicate column name 'first_property'

now the question: is there a syntax for sql to skip that name if it exist

thanks for any help.

Hello,

you can use IF ELSE condition of Transact SQL

IF Boolean_expression 
     { sql_statement | statement_block } 
[ ELSE 
     { sql_statement | statement_block } ]

an example can be like the following;

DECLARE @compareprice money, @cost money 
EXECUTE Production.uspGetList '%Bikes%', 700, 
    @compareprice OUT, 
    @cost OUTPUT
IF @cost <= @compareprice 
BEGIN
    PRINT 'These products can be purchased for less than 
    $'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'
END
ELSE
    PRINT 'The prices for all products in this category exceed 
    $'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'

Hope this will help you, find the Property name and match it with your new entries.... Hope this will help you.

whoa. i think that is a little different from what i needed. what i am doing is throwing the mysql query in to phpmyadmin directly.

from what i see there is it looks like that is calling from the DB. but i am not sure. that is a little beyond me what is going on there.

Member Avatar for Member #120589

Why do you need to create a new field? You seem to be adding a new field instead of a new record. Could you elaborate on the implementation?

Why do you need to create a new field? You seem to be adding a new field instead of a new record. Could you elaborate on the implementation?

np. what is going on is simple. i am just adding fields to a table that i am still building by using the alter table and add function. and i have multiple forms that push and pull data from the DB.

i made a little script which will put all the values from that form into the syntax for mysql. then i take it and just so to phpmyadmin and dump that data into the sql.

but i get that the name is a duplicate and was wondering if there was a way that i could make the query skip adding that value to the table if it already exist in the table

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.