Howdy_McGee
Howdy_McGee

Reputation: 10653

Make Column have Same Value as Similar Column

I'm using phpMyAdmin and I was wondering if there is a way I can default a column to have the same value as another column?

Scenario

So I'm running a wordpress and I want to give users access to a page for a certain amount of time so I created a trialTime column. I want to default the value of trailTime to the users registrationDate (which is also a column). This way I can start the countdown from when they register but the admin will be able to reset the value of trialTime inside the admin panel if they want to.

Upvotes: 1

Views: 79

Answers (4)

Marcus Adams
Marcus Adams

Reputation: 53860

A default column value in MySQL must be a constant, with the exception that you can use CURRENT_TIMESTAMP for a TIMESTAMP column.

If the record is created at the time of registration, then using a TIMESTAMP column with CURRENT_TIMESTAMP may suffice.

Alternatively, you could handle this when reading the value from the database, something like this:

SELECT IFNULL(trialTime, registrationDate) AS trialTime

I'm assuming that the values could differ, otherwise you wouldn't need the trialTime column at all.

Upvotes: 0

gintas
gintas

Reputation: 2178

If you are using wordpress and want to set certain user field at time of registration, just use *user_register* action hook.

function set_that_additional_thing ($user_id) {
     global $wpdb;
     $wpdb->query("UPDATE `" . $wpdb->prefix . "users` 
                   SET `copy_field` = `field` 
                   WHERE `user_id` = " . $user_id);
}

add_action('user_register', 'set_that_additional_thing ');

Upvotes: 2

Joe Stefanelli
Joe Stefanelli

Reputation: 135818

Seems like you might be overthinking the problem. Why not just insert the same value into both columns at the time the registration is created.

Upvotes: 0

swt83
swt83

Reputation: 2011

I'm not a Wordpress whiz, but I wonder if you could manually code this using the WPDB class. It allows total control over the database.

http://codex.wordpress.org/Class_Reference/wpdb

Upvotes: 0

Related Questions