ULtimateG
ULtimateG

Reputation: 3

How to use date for a new column name

I am trying to add a new column to my MYSQLi table using PHP. I am unsure how to alter my table. I want to use data from Time to add new column like this

Time | 2016-Mar-06-22:40 2016-Mar-06-22:40 | YES

Edit : And when the time has change, a new column will create like this

Time | 2016-Mar-06-22:40 | 2016-Mar-06-23:00 2016-Mar-06-23:00| YES | NO

This is my code

 $MyTime = date("Y-M-d-H:i"); 
 $sql = $conn->query("ALTER TABLE MyTable ADD $MyTime enum('YES', 'NO') "); 

I tried to add 2016-Mar-06-22:40 in phpMyAdmin and it's done, but when I try to make it in php mysqli code, it didn't create new column for me.

Thanks in advance

Upvotes: 0

Views: 156

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157863

The answer is simple: you should never ever add any column that looks like a datetime. Such values have to be stored as data values, not column names.

What you probably want is a related table to store datetime values.

The database you are using is called relational for a reason. Means you don't have to store all the required data in the same table. Instead ,you can use several tables while installing relations between them.

So, you probably need a table like this

main_table_id | datetime          | value

and then insert into this table any number or records like

1             | 2016-Mar-06-22:40 | YES

Upvotes: 5

Related Questions