I have written myself a PHP script to help manage a small squad of 10 members. This script current connects to a database and updates weekly updating any rank and power change. However I want to expand this and in a new table, have all the power gained kept in a separate column instead of adding it all up.
The only way I can think of doing this is by using the below code to create the column and changing the name for each.
ALTER TABLE `power gained` ADD timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER name;
So my question is, can I create columns making sure their name is unique, OR is it possible to set a column name to the timestamp as that would save me a column.
I'd like to also make it clear I am using mysql_ functions as this is on my local system, not vulnerable to attacks and I am not stupid enough to make mistakes.
Use normalisation.
Split the users and the power into two difference tables.
CREATE TABLE `users_power` (
`user_id` smallint(5) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`total_power` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
CREATE TABLE `power` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` smallint(1) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`power_increase` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
DROP TRIGGER IF EXISTS `Update users_power`;
CREATE TRIGGER `Update users_power` AFTER INSERT ON `power` FOR EACH ROW BEGIN
UPDATE `users_power` AS `UP`
SET `UP`.`total_power` = `UP`.`total_power` + NEW.`power_increase`
WHERE `UP`.`user_id` = NEW.`user_id`;
END
And then to look up, do something like;
SELECT `power` AS total_power
FROM users_power
WHERE users_power.`user_id` = 1
And your structure would look something like;
select * from power where user_id = 1;
+----+---------+---------------------+----------------+
| id | user_id | time | power_increase |
+----+---------+---------------------+----------------+
| 1 | 1 | 2014-08-7 17:04:06 | 5 |
| 2 | 1 | 2014-08-15 17:04:31 | 15 |
+----+---------+---------------------+----------------+
2 rows in set
select * from users_power where user_id = 1;
+---------+------+-------------+
| user_id | name | total_power |
+---------+------+-------------+
| 1 | joe | 21 |
+---------+------+-------------+
1 row in set
power
Haven't yet tested this but I think I have found a way I can do this in a way that I understand (which is the most important part).
$now = date('Y-m-d');
mysql_query("ALTER TABLE `power gained` ADD `$now` VARCHAR(22) AFTER name");
// ...
mysql_query("UPDATE `power gained` (`$now`) VALUES($gained) WHERE id = $i");
I'm not looking to extract the data from the database as I can access it just by going to localhost/phpmyadmin and I'm not going to ever need to extract it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.