Reputation: 19
I wanna add an extra column to my database and give it the name price_23-05-2019
.
How do I put this into a working query?
I have this right now, which is clearly not working:
$date = date("d-m-Y");
$query =
"ALTER TABLE `products_05_2019`
ADD price_'.$date.' DECIMAL(7,2)";
($result = mysqli_query($link, $query))
Upvotes: 0
Views: 72
Reputation: 2005
As @ficuscr says above, you may want to have a look at the design of your database so you don't have to create columns from code.
Anyway, what I use to do when I have a column name depending on code is create a new variable and then include it into the query:
$date = date("d-m-Y");
$column_name = 'price_'.$date;
$query = "ALTER TABLE `products_05_2019` ADD `$column_name` DECIMAL(7,2)";
Upvotes: 0
Reputation: 780673
You really shouldn't have separate columns for each date. There should just be a date
column, with the date as the value, and a separate row for each date.
But if you have to do it this way, here's how to solve it.
If you use -
in a column name, you have to enclose the name in backticks.
$date = date("d-m-Y");
$query =
"ALTER TABLE `products_05_2019`
ADD `price_$date` DECIMAL(7,2)";
$result = mysqli_query($link, $query);
But it would probably be better to use _
instead of -
.
$date = date("d_m_Y");
$query =
"ALTER TABLE `products_05_2019`
ADD price_'.$date.' DECIMAL(7,2)";
$result = mysqli_query($link, $query);
Upvotes: 2