Discussion:
Creating / Inserting into Table named after WP Username
BenderisGreat
2013-08-24 23:22:46 UTC
Permalink
I am playing with something, and need a snippet of code. I am trying to have
a php form submit data into a table, a table with the same name as the
current user. Can someone help me out and share the call for this?

This code works to call information from the database;

<?php $result = mysql_query("SELECT * FROM $current_user->user_login",
$connection);


But in the submit.php, it does not work in the INSERT INTO $current_user
etc...



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Creating-Inserting-into-Table-named-after-WP-Username-tp42095.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Chris McCoy
2013-08-24 23:25:45 UTC
Permalink
did you add the global $current_user;
?
Post by BenderisGreat
I am playing with something, and need a snippet of code. I am trying to have
a php form submit data into a table, a table with the same name as the
current user. Can someone help me out and share the call for this?
This code works to call information from the database;
<?php $result = mysql_query("SELECT * FROM $current_user->user_login",
$connection);
But in the submit.php, it does not work in the INSERT INTO $current_user
etc...
--
http://wordpress-hackers.1065353.n5.nabble.com/Creating-Inserting-into-Tab
le-named-after-WP-Username-tp42095.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
BenderisGreat
2013-08-24 23:40:01 UTC
Permalink
I have tried it with no success;


$sql="INSERT INTO 'tracking_users' '.GLOBAL $current_user->user_login.'
VALUES ($_POST[firstname]')";

as well as :

$db_name = $current_user->user_login;
function addit() {
GLOBAL $db_name;
}

$sql="INSERT INTO 'tracking_users' ('.$db_name.') VALUES
($_POST[firstname]')";



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Creating-Inserting-into-Table-named-after-WP-Username-tp42095p42097.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
BenderisGreat
2013-08-25 00:23:20 UTC
Permalink
This isnt working, I need to grab the active logged in user via Sql but I am
unsure how.



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Creating-Inserting-into-Table-named-after-WP-Username-tp42095p42098.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Abdussamad Abdurrazzaq
2013-08-25 00:26:40 UTC
Permalink
As I said in my last message your usage of the global keyword is
incorrect and creating a separate table for each user is probably not a
good idea.
Post by BenderisGreat
This isnt working, I need to grab the active logged in user via Sql but I am
unsure how.
--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Creating-Inserting-into-Table-named-after-WP-Username-tp42095p42098.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Ryan McCue
2013-08-25 01:42:07 UTC
Permalink
Post by BenderisGreat
This isnt working, I need to grab the active logged in user via Sql but I am
unsure how.
I'm not quite sure what you're trying to do here, but you can't get the
current user via SQL, it's set via cookies.

If you're doing this in a plugin/theme, then please don't use raw SQL
queries for this. The global $current_user contains all the data you'll
need, and if you need to store custom data for the user, please use user
meta instead of custom tables.

If you're doing this in some separate application, you need to load WP
in and use the authentication functions to get at the data.

More information would be helpful so that we know what you're trying to do.
--
Ryan McCue
<http://ryanmccue.info/>
William P. Davis
2013-08-25 03:19:34 UTC
Permalink
Can you explain what you're trying to do here? We might be able to find a method that doesn't need to create a table for each user, which would get around this problem entirely.

Will
Sent from my BlackBerry

-----Original Message-----
From: Ryan McCue <***@rotorised.com>
Sender: "wp-hackers" <wp-hackers-***@lists.automattic.com>Date: Sun, 25 Aug 2013 11:42:07
To: <wp-***@lists.automattic.com>
Reply-To: wp-***@lists.automattic.com
Subject: Re: [wp-hackers] Creating / Inserting into Table named after WP
Username
Post by BenderisGreat
This isnt working, I need to grab the active logged in user via Sql but I am
unsure how.
I'm not quite sure what you're trying to do here, but you can't get the
current user via SQL, it's set via cookies.

If you're doing this in a plugin/theme, then please don't use raw SQL
queries for this. The global $current_user contains all the data you'll
need, and if you need to store custom data for the user, please use user
meta instead of custom tables.

If you're doing this in some separate application, you need to load WP
in and use the authentication functions to get at the data.

More information would be helpful so that we know what you're trying to do.
--
Ryan McCue
<http://ryanmccue.info/>
Gregory Lancaster
2013-08-25 05:55:09 UTC
Permalink
Woah thanks for the help guys. @Ryan - I am storing custom data, but the
user meta only allows for me to store two fields. The custom data need to
use the INSERT INTO so I have a list of data entered for each user. Right
now I am trying to figure how I can hook into (terminology right?) the
wp_users and create a new table in a sep database for each user that
registers.

The idea is to create a profile page where users can store more data than
they can normally. Upload more than one picture etc. If there is a more
intelligent way to do this PLEASE share! The user_meta is limited though,
thats why I opted to make my own db.
Post by William P. Davis
Can you explain what you're trying to do here? We might be able to find a
method that doesn't need to create a table for each user, which would get
around this problem entirely.
Will
Sent from my BlackBerry
-----Original Message-----
25 Aug 2013 11:42:07
Subject: Re: [wp-hackers] Creating / Inserting into Table named after WP
Username
Post by BenderisGreat
This isnt working, I need to grab the active logged in user via Sql but
I am
Post by BenderisGreat
unsure how.
I'm not quite sure what you're trying to do here, but you can't get the
current user via SQL, it's set via cookies.
If you're doing this in a plugin/theme, then please don't use raw SQL
queries for this. The global $current_user contains all the data you'll
need, and if you need to store custom data for the user, please use user
meta instead of custom tables.
If you're doing this in some separate application, you need to load WP
in and use the authentication functions to get at the data.
More information would be helpful so that we know what you're trying to do.
--
Ryan McCue
<http://ryanmccue.info/>
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Ryan McCue
2013-08-25 05:59:02 UTC
Permalink
Post by Gregory Lancaster
The idea is to create a profile page where users can store more data than
they can normally. Upload more than one picture etc. If there is a more
intelligent way to do this PLEASE share! The user_meta is limited though,
thats why I opted to make my own db.
You can store arrays or objects in the meta fields, which is the
preferred way to handle something like this. Creating extra tables is
not the correct way to do it.

(Internally, these are run through serialize(), then unserialize() on
access.)
--
Ryan McCue
<http://ryanmccue.info/>
Gregory Lancaster
2013-08-25 06:06:12 UTC
Permalink
The way the information is being stored, and the information being stored,
really is easier with a database. I need a historical view of everything a
user entered, along with a date stamp. SQL makes this simple. Why is a
separate DB a bad idea? It is fully functional right now, with the
exception of it being dynamic per user. Cant figure how to make use of the
*$current_user->user_login; *outside the loop in php. I am trying to use
it to create a table name that matches the current username.
Post by Gregory Lancaster
Post by Gregory Lancaster
The idea is to create a profile page where users can store more data than
they can normally. Upload more than one picture etc. If there is a more
intelligent way to do this PLEASE share! The user_meta is limited
though,
Post by Gregory Lancaster
thats why I opted to make my own db.
You can store arrays or objects in the meta fields, which is the
preferred way to handle something like this. Creating extra tables is
not the correct way to do it.
(Internally, these are run through serialize(), then unserialize() on
access.)
--
Ryan McCue
<http://ryanmccue.info/>
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Dion Hulse (dd32)
2013-08-25 06:20:48 UTC
Permalink
1. Use a single table.. no table-per-user.. ie. { user_id, timestamp,
meta_key, meta_value }

2. Basic PHP:
function whatever() {
global $current_user;
return $wpdb->insert( "my_table", array( 'user_id' => $current_user->ID,
'timestamp' => time(), 'meta_key' => 'something', 'meta_value' =>
'whatever' ) );
}
Post by Gregory Lancaster
The way the information is being stored, and the information being stored,
really is easier with a database. I need a historical view of everything a
user entered, along with a date stamp. SQL makes this simple. Why is a
separate DB a bad idea? It is fully functional right now, with the
exception of it being dynamic per user. Cant figure how to make use of the
*$current_user->user_login; *outside the loop in php. I am trying to use
it to create a table name that matches the current username.
Post by Gregory Lancaster
Post by Gregory Lancaster
The idea is to create a profile page where users can store more data
than
Post by Gregory Lancaster
Post by Gregory Lancaster
they can normally. Upload more than one picture etc. If there is a
more
Post by Gregory Lancaster
Post by Gregory Lancaster
intelligent way to do this PLEASE share! The user_meta is limited
though,
Post by Gregory Lancaster
thats why I opted to make my own db.
You can store arrays or objects in the meta fields, which is the
preferred way to handle something like this. Creating extra tables is
not the correct way to do it.
(Internally, these are run through serialize(), then unserialize() on
access.)
--
Ryan McCue
<http://ryanmccue.info/>
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Gregory Lancaster
2013-08-25 09:16:26 UTC
Permalink
I still don't understand the benefit of using usermeta vs my own database.
I googled for information about it but don't see any strong opinions one
way or the other.

The only reason I am doing it this way is because I want to have a
historical record - Meaning show the whole table, including all rows, which
will be dated and include several rows. wp_usermeta only allows for one
value per key and updating it results in the loss of the previous entry.
That wont work for me.

Additionally, assuming I create a new table in the wordpress database and
associated each entry with the user->id that would still be a very messy
way to store the data for hundreds of users who all have 20-40 entries
each. Am I missing something here?
Post by Dion Hulse (dd32)
1. Use a single table.. no table-per-user.. ie. { user_id, timestamp,
meta_key, meta_value }
function whatever() {
global $current_user;
return $wpdb->insert( "my_table", array( 'user_id' => $current_user->ID,
'timestamp' => time(), 'meta_key' => 'something', 'meta_value' =>
'whatever' ) );
}
Post by Gregory Lancaster
The way the information is being stored, and the information being
stored,
Post by Gregory Lancaster
really is easier with a database. I need a historical view of
everything a
Post by Gregory Lancaster
user entered, along with a date stamp. SQL makes this simple. Why is a
separate DB a bad idea? It is fully functional right now, with the
exception of it being dynamic per user. Cant figure how to make use of
the
Post by Gregory Lancaster
*$current_user->user_login; *outside the loop in php. I am trying to use
it to create a table name that matches the current username.
Post by Gregory Lancaster
Post by Gregory Lancaster
The idea is to create a profile page where users can store more data
than
Post by Gregory Lancaster
Post by Gregory Lancaster
they can normally. Upload more than one picture etc. If there is a
more
Post by Gregory Lancaster
Post by Gregory Lancaster
intelligent way to do this PLEASE share! The user_meta is limited
though,
Post by Gregory Lancaster
thats why I opted to make my own db.
You can store arrays or objects in the meta fields, which is the
preferred way to handle something like this. Creating extra tables is
not the correct way to do it.
(Internally, these are run through serialize(), then unserialize() on
access.)
--
Ryan McCue
<http://ryanmccue.info/>
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2013-08-25 12:50:42 UTC
Permalink
Post by Gregory Lancaster
I still don't understand the benefit of using usermeta vs my own database.
I googled for information about it but don't see any strong opinions one
way or the other.
The only reason I am doing it this way is because I want to have a
historical record - Meaning show the whole table, including all rows, which
will be dated and include several rows. wp_usermeta only allows for one
value per key and updating it results in the loss of the previous entry.
That wont work for me.
In this case, I'd say it makes sense to use a custom table, not usermeta.
Post by Gregory Lancaster
Additionally, assuming I create a new table in the wordpress database and
associated each entry with the user->id that would still be a very messy
way to store the data for hundreds of users who all have 20-40 entries
each. Am I missing something here?
I guess it's partly a matter of opinion, but I'd say it would be much messier to have hundreds of tables in my database, each with only 20-40 entries in them. Also, if you feel you must do it this way, use a unique prefix for the tables. Otherwise you are just asking for trouble, if someone registers on your site with a username that just happens to match the name of one of your other tables.
J.D. Grimes
2013-08-25 15:49:39 UTC
Permalink
Post by Gregory Lancaster
The only reason I am doing it this way is because I want to have a
historical record - Meaning show the whole table, including all rows, which
will be dated and include several rows. wp_usermeta only allows for one
value per key and updating it results in the loss of the previous entry.
That wont work for me.
I just wanted to add another comment on this. You *can* have multiple values for the same key in the usermeta table. There can be multiple rows with the same meta_key, and different meta_values. I just wanted to clarify that. So if you wanted to you could store all of your values in the usermeta table with duplicate keys, and add a date as part of the value for each record (as part of an array, for example). But it still sounds to me that in your case, a separate table with a date column would be better.
Guus (IFS)
2013-08-26 04:49:12 UTC
Permalink
I did not folllow the whole conversation, but I understand you want to
create a table per user? I don't think that's a good idea, but you could
make an additional table and make the user id the index and store then
something like user id, meta key, meta value, start date, end date.

----- Original Message -----
From: "J.D. Grimes" <***@codesymphony.co>
To: <wp-***@lists.automattic.com>
Sent: Sunday, August 25, 2013 11:49 PM
Subject: Re: [wp-hackers] Creating / Inserting into Table named after
WPUsername
Post by J.D. Grimes
Post by Gregory Lancaster
The only reason I am doing it this way is because I want to have a
historical record - Meaning show the whole table, including all rows, which
will be dated and include several rows. wp_usermeta only allows for one
value per key and updating it results in the loss of the previous entry.
That wont work for me.
I just wanted to add another comment on this. You *can* have multiple
values for the same key in the usermeta table. There can be multiple rows
with the same meta_key, and different meta_values. I just wanted to
clarify that. So if you wanted to you could store all of your values in
the usermeta table with duplicate keys, and add a date as part of the
value for each record (as part of an array, for example). But it still
sounds to me that in your case, a separate table with a date column would
be better.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Abdussamad Abdurrazzaq
2013-08-25 00:23:38 UTC
Permalink
Your php is incorrect:

http://www.php.net/manual/en/language.variables.scope.php
Post by BenderisGreat
I have tried it with no success;
$sql="INSERT INTO 'tracking_users' '.GLOBAL $current_user->user_login.'
VALUES ($_POST[firstname]')";
$db_name = $current_user->user_login;
function addit() {
GLOBAL $db_name;
}
$sql="INSERT INTO 'tracking_users' ('.$db_name.') VALUES
($_POST[firstname]')";
--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Creating-Inserting-into-Table-named-after-WP-Username-tp42095p42097.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Gregory Lancaster
2013-08-26 16:46:22 UTC
Permalink
so i still have the same issue- how to steup the sql to create the table so
it matches the current active user.
User id not the index of course, but a field (indexed would be nicest).
----- Original Message ----- From: "Guus (IFS)" <
Sent: Monday, August 26, 2013 12:49 PM
Subject: Re: [wp-hackers] Creating / Inserting into Table named
afterWPUsername
I did not folllow the whole conversation, but I understand you want to
Post by Guus (IFS)
create a table per user? I don't think that's a good idea, but you could
make an additional table and make the user id the index and store then
something like user id, meta key, meta value, start date, end date.
Sent: Sunday, August 25, 2013 11:49 PM
Subject: Re: [wp-hackers] Creating / Inserting into Table named after
WPUsername
The only reason I am doing it this way is because I want to have a
Post by J.D. Grimes
Post by Gregory Lancaster
historical record - Meaning show the whole table, including all rows,
which
will be dated and include several rows. wp_usermeta only allows for one
value per key and updating it results in the loss of the previous entry.
That wont work for me.
I just wanted to add another comment on this. You *can* have multiple
values for the same key in the usermeta table. There can be multiple rows
with the same meta_key, and different meta_values. I just wanted to
clarify that. So if you wanted to you could store all of your values in
the usermeta table with duplicate keys, and add a date as part of the
value for each record (as part of an array, for example). But it still
sounds to me that in your case, a separate table with a date column would
be better.
______________________________**_________________
wp-hackers mailing list
http://lists.automattic.com/**mailman/listinfo/wp-hackers<http://lists.automattic.com/mailman/listinfo/wp-hackers>
______________________________**_________________
wp-hackers mailing list
http://lists.automattic.com/**mailman/listinfo/wp-hackers<http://lists.automattic.com/mailman/listinfo/wp-hackers>
______________________________**_________________
wp-hackers mailing list
http://lists.automattic.com/**mailman/listinfo/wp-hackers<http://lists.automattic.com/mailman/listinfo/wp-hackers>
Otto
2013-08-26 16:53:23 UTC
Permalink
On Mon, Aug 26, 2013 at 11:46 AM, Gregory Lancaster
Post by Gregory Lancaster
so i still have the same issue- how to steup the sql to create the table so
it matches the current active user.
You shouldn't have one table per user. There's no case where that makes sense.

Instead, take your existing table structure, and add a user_id column
onto it to connect the various rows with a specific user.

-Otto
Gregory Lancaster
2013-08-26 17:53:45 UTC
Permalink
I considered that, but I have no idea how to match the user id owning a
row- I am trying to figure that out now. Its actually why I posted here,
looking for a way to grab the active user ID or login_name and match it to
a table or row. I tried INSERT INTO $table and that wont work, nor will
any variation with quotes or curly brackets.
Post by Otto
On Mon, Aug 26, 2013 at 11:46 AM, Gregory Lancaster
Post by Gregory Lancaster
so i still have the same issue- how to steup the sql to create the table
so
Post by Gregory Lancaster
it matches the current active user.
You shouldn't have one table per user. There's no case where that makes sense.
Instead, take your existing table structure, and add a user_id column
onto it to connect the various rows with a specific user.
-Otto
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Nicholas Ciske
2013-08-26 18:11:28 UTC
Permalink
Use one row per entry, each row has a user (user_id) and date (as well as a auto incrementing primary key).

id - auto-incrementing primary key
user_id - WP user ID (index this column)
entry_date - date this entry was made (could be a date YYYYMMDD or unix timestamp depending on your needs)
(add other columns as needed)

get all rows for a user
SELECT * FROM table_name WHERE user_id = '1';

insert a new row for a user
INSERT INTO table_name (user_id, entry_date, ... ) values ( '1', CURDATE, ... ); // this is just psedocode

You can also query this schema by date (e.g. all entries between dates for a user or all users, etc).
Post by Gregory Lancaster
I tried INSERT INTO $table and that wont work, nor will
any variation with quotes or curly brackets.
If you're getting errors, sharing those error messages, along with your DB schema and relvant code will go a long way towards getting help -- right now you’re getting high level advice as there's not way to give specific feedback.

_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske
Post by Gregory Lancaster
I considered that, but I have no idea how to match the user id owning a
row- I am trying to figure that out now. Its actually why I posted here,
looking for a way to grab the active user ID or login_name and match it to
a table or row. I tried INSERT INTO $table and that wont work, nor will
any variation with quotes or curly brackets.
Otto
2013-08-26 18:11:35 UTC
Permalink
I guess I'm not really understanding the question or the underlying
problem here then. If you have a table called, say, wp_userstuff
(using the table prefix), and it looks like this:

(
`user_id` bigint(20) NOT NULL,
`otherstuff` text NOT NULL
)

Then you can select from it like so:

$current_user = wp_get_current_user();
if ( $current_user->exists() ) {
$results = $wpdb->get_results( $wpdb->prepare("SELECT * FROM
{$wpdb->prefix}userstuff WHERE user_id = %d", $current_user->ID) );
}

To insert into the table, you could do something similar with
$wpdb->query(), but the $wpdb->insert() function is probably better.

$wpdb->insert( $wpdb->prefix.'userstuff', array( 'user_id' =>
$current_user->ID, 'otherstuff' => 'whatever' ) );

But realistically, usermeta is going to be a better choice if this is
something you need to pull out for every user lookup, since usermeta
is pulled automatically. The only reason to make a separate table for
it is if you have special needs. Your mention of storing "history"
would be a valid need, because you could stick a datetime column on it
to store that information as well.

-Otto



On Mon, Aug 26, 2013 at 12:53 PM, Gregory Lancaster
Post by Gregory Lancaster
I considered that, but I have no idea how to match the user id owning a
row- I am trying to figure that out now. Its actually why I posted here,
looking for a way to grab the active user ID or login_name and match it to
a table or row. I tried INSERT INTO $table and that wont work, nor will
any variation with quotes or curly brackets.
Post by Otto
On Mon, Aug 26, 2013 at 11:46 AM, Gregory Lancaster
Post by Gregory Lancaster
so i still have the same issue- how to steup the sql to create the table
so
Post by Gregory Lancaster
it matches the current active user.
You shouldn't have one table per user. There's no case where that makes sense.
Instead, take your existing table structure, and add a user_id column
onto it to connect the various rows with a specific user.
-Otto
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Gregory Lancaster
2013-08-26 18:48:57 UTC
Permalink
i appreiate the feedback. The issue I am obviously explaining very poorly
is that I do not understand how to copy the wordpress ->wp_users ->ID field
into a separate database. Someone had mentioned hooking into the register
new user code so everytime someone registers new it creates a new table or
row whatever in a sep database with a matching user id. That is what I am
unclear on how to do.
Post by Otto
I guess I'm not really understanding the question or the underlying
problem here then. If you have a table called, say, wp_userstuff
(
`user_id` bigint(20) NOT NULL,
`otherstuff` text NOT NULL
)
$current_user = wp_get_current_user();
if ( $current_user->exists() ) {
$results = $wpdb->get_results( $wpdb->prepare("SELECT * FROM
{$wpdb->prefix}userstuff WHERE user_id = %d", $current_user->ID) );
}
To insert into the table, you could do something similar with
$wpdb->query(), but the $wpdb->insert() function is probably better.
$wpdb->insert( $wpdb->prefix.'userstuff', array( 'user_id' =>
$current_user->ID, 'otherstuff' => 'whatever' ) );
But realistically, usermeta is going to be a better choice if this is
something you need to pull out for every user lookup, since usermeta
is pulled automatically. The only reason to make a separate table for
it is if you have special needs. Your mention of storing "history"
would be a valid need, because you could stick a datetime column on it
to store that information as well.
-Otto
On Mon, Aug 26, 2013 at 12:53 PM, Gregory Lancaster
Post by Gregory Lancaster
I considered that, but I have no idea how to match the user id owning a
row- I am trying to figure that out now. Its actually why I posted here,
looking for a way to grab the active user ID or login_name and match it
to
Post by Gregory Lancaster
a table or row. I tried INSERT INTO $table and that wont work, nor will
any variation with quotes or curly brackets.
Post by Otto
On Mon, Aug 26, 2013 at 11:46 AM, Gregory Lancaster
Post by Gregory Lancaster
so i still have the same issue- how to steup the sql to create the
table
Post by Gregory Lancaster
Post by Otto
so
Post by Gregory Lancaster
it matches the current active user.
You shouldn't have one table per user. There's no case where that makes sense.
Instead, take your existing table structure, and add a user_id column
onto it to connect the various rows with a specific user.
-Otto
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Nicholas Ciske
2013-08-26 18:55:13 UTC
Permalink
Ah, you want the hooks related to this... here are the big ones:

New user added to system:
http://codex.wordpress.org/Plugin_API/Action_Reference/user_register

If you want to show something on the user profile page (e.g. the history you mentioned):
show_user_profile

See also:
These hooks relate to users being edited/updated/deleted.

profile_update

edit_user_profile

delete_user

_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske
Post by Gregory Lancaster
i appreiate the feedback. The issue I am obviously explaining very poorly
is that I do not understand how to copy the wordpress ->wp_users ->ID field
into a separate database. Someone had mentioned hooking into the register
new user code so everytime someone registers new it creates a new table or
row whatever in a sep database with a matching user id. That is what I am
unclear on how to do.
J.D. Grimes
2013-08-26 18:58:06 UTC
Permalink
Post by Gregory Lancaster
i appreiate the feedback. The issue I am obviously explaining very poorly
is that I do not understand how to copy the wordpress ->wp_users ->ID field
into a separate database. Someone had mentioned hooking into the register
new user code so everytime someone registers new it creates a new table or
row whatever in a sep database with a matching user id. That is what I am
unclear on how to do.
It sounds like you are trying to set up this metadata in the table(s) ahead of time (before the data is ever called for). Why do you need to do that? The general way of doing things is that you check if the data exists in the database, and if not, then you add defaults or whatever. In other words, why do you need to set all of the user's data up ahead of time instead of just setting it up as needed on the fly? That is the best way to do it in my opinion.
Gregory Lancaster
2013-08-26 19:06:06 UTC
Permalink
When you say on the fly, you mean (for example) when a user registers, have
the user ID copied over into a sep database? That is actually what I am
trying to figure out haha. Id like to have the user->ID copied into a
separate database row *(or create a new table with the user-ID as the table
name)* after registration. If I could figure that out then I imagine the
rest would fall into place for me. The WP loop is something I am still
learning, so I really appreciate the reference information *Nicholas. *

*J.D* - Is that what you meant? Did I understand your question correctly?

if current_user->ID if exists fetch array($result) echo $row[1] etc...
Post by Gregory Lancaster
Post by Gregory Lancaster
i appreiate the feedback. The issue I am obviously explaining very
poorly
Post by Gregory Lancaster
is that I do not understand how to copy the wordpress ->wp_users ->ID
field
Post by Gregory Lancaster
into a separate database. Someone had mentioned hooking into the
register
Post by Gregory Lancaster
new user code so everytime someone registers new it creates a new table
or
Post by Gregory Lancaster
row whatever in a sep database with a matching user id. That is what I
am
Post by Gregory Lancaster
unclear on how to do.
It sounds like you are trying to set up this metadata in the table(s)
ahead of time (before the data is ever called for). Why do you need to do
that? The general way of doing things is that you check if the data exists
in the database, and if not, then you add defaults or whatever. In other
words, why do you need to set all of the user's data up ahead of time
instead of just setting it up as needed on the fly? That is the best way to
do it in my opinion.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2013-08-26 19:17:07 UTC
Permalink
Post by Gregory Lancaster
When you say on the fly, you mean (for example) when a user registers, have
the user ID copied over into a sep database? That is actually what I am
trying to figure out haha. Id like to have the user->ID copied into a
separate database row *(or create a new table with the user-ID as the table
name)* after registration. If I could figure that out then I imagine the
rest would fall into place for me. The WP loop is something I am still
learning, so I really appreciate the reference information *Nicholas. *
*J.D* - Is that what you meant? Did I understand your question correctly?
if current_user->ID if exists fetch array($result) echo $row[1] etc...
Not exactly what I mean. My question was why are you copying the user_ID over at that time - but that may not really be important.

If you need to add some information do the database when a user registers, then you wan to use the user_register action hook as Nicholas said.

http://codex.wordpress.org/Plugin_API/Action_Reference/user_register

But in your function hooked to that hook, you do *not* want to use the ID of the current user. You need to use the ID of the user that has just registered, which will be passed as the first parameter to your hooked function, as shown in the examples there.

Back to my question, though, I'm not sure why you feel you need to do this at the time the user registers. I assume that it has something to do with the date/time oriented aspect of the data that you want to store? I.e., I guess you have some entries that need to go in at that time so they will be dated properly? But you are going to have to do this differently for registered users...
Gregory Lancaster
2013-08-26 19:28:23 UTC
Permalink
OH I understand what you're asking.

I want to pass the User_ID to the new table upon registering because I am
coding this for a profile page of sorts. So after registering, and
confirming registration they will load up say site.com/profile which will
dynamically pull their information from a separate table using their
user_id. From that page they can enter different information that would be
saved in this separate database (for each user).

So the extra information they will be adding does not need to be added
right away, however to load the profile page with their display_name and
basic information I do need the information passed. Now that you bring it
up however, I suppose I could have the profile page just check if the
current user_id exists, and if yes then show the basic profile page with
just their display_name etc. But either way, once they enter information
into the on page form and submit it, the user-ID needs to be passed.

Which is where I came up with passing the current user ID, since they would
already be logged in and registered. Really most of the questions I asked
in this thread are variations of "how can I take the user_id and pass it to
a separate database upon registration (or upon form submit) so the
information in their specific table can be dumped back on their profile
page for viewing.

The information will (ideally) be outputted into a format where I could
format it with google charts or some type of visual display for them.

I hope that clarifies man, and again I am new to the loop so I apologize if
I am driving anyone insane here. Any advice welcome :)
Post by J.D. Grimes
Post by Gregory Lancaster
When you say on the fly, you mean (for example) when a user registers,
have
Post by Gregory Lancaster
the user ID copied over into a sep database? That is actually what I am
trying to figure out haha. Id like to have the user->ID copied into a
separate database row *(or create a new table with the user-ID as the
table
Post by Gregory Lancaster
name)* after registration. If I could figure that out then I imagine the
rest would fall into place for me. The WP loop is something I am still
learning, so I really appreciate the reference information *Nicholas. *
*J.D* - Is that what you meant? Did I understand your question correctly?
if current_user->ID if exists fetch array($result) echo $row[1] etc...
Not exactly what I mean. My question was why are you copying the user_ID
over at that time - but that may not really be important.
If you need to add some information do the database when a user registers,
then you wan to use the user_register action hook as Nicholas said.
http://codex.wordpress.org/Plugin_API/Action_Reference/user_register
But in your function hooked to that hook, you do *not* want to use the ID
of the current user. You need to use the ID of the user that has just
registered, which will be passed as the first parameter to your hooked
function, as shown in the examples there.
Back to my question, though, I'm not sure why you feel you need to do this
at the time the user registers. I assume that it has something to do with
the date/time oriented aspect of the data that you want to store? I.e., I
guess you have some entries that need to go in at that time so they will be
dated properly? But you are going to have to do this differently for
registered users...
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Post by Gregory Lancaster
When you say on the fly, you mean (for example) when a user registers,
have
Post by Gregory Lancaster
the user ID copied over into a sep database? That is actually what I am
trying to figure out haha. Id like to have the user->ID copied into a
separate database row *(or create a new table with the user-ID as the
table
Post by Gregory Lancaster
name)* after registration. If I could figure that out then I imagine the
rest would fall into place for me. The WP loop is something I am still
learning, so I really appreciate the reference information *Nicholas. *
*J.D* - Is that what you meant? Did I understand your question correctly?
if current_user->ID if exists fetch array($result) echo $row[1] etc...
Not exactly what I mean. My question was why are you copying the user_ID
over at that time - but that may not really be important.
If you need to add some information do the database when a user registers,
then you wan to use the user_register action hook as Nicholas said.
http://codex.wordpress.org/Plugin_API/Action_Reference/user_register
But in your function hooked to that hook, you do *not* want to use the ID
of the current user. You need to use the ID of the user that has just
registered, which will be passed as the first parameter to your hooked
function, as shown in the examples there.
Back to my question, though, I'm not sure why you feel you need to do this
at the time the user registers. I assume that it has something to do with
the date/time oriented aspect of the data that you want to store? I.e., I
guess you have some entries that need to go in at that time so they will be
dated properly? But you are going to have to do this differently for
registered users...
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Gregory Lancaster
2013-08-26 19:33:56 UTC
Permalink
I should add, that the on page form used to submit this information is used
multiple times for dated information input. So if I use one table rather
than a table for each user_id then every row (and every button submit)
needs to grab the user-ID and input that with the form information so it
can be extracted. Thats why I was opting for table names prefixed with the
user-id rather than rows with a user ID column.


On Mon, Aug 26, 2013 at 12:28 PM, Gregory Lancaster <
Post by Gregory Lancaster
OH I understand what you're asking.
I want to pass the User_ID to the new table upon registering because I am
coding this for a profile page of sorts. So after registering, and
confirming registration they will load up say site.com/profile which will
dynamically pull their information from a separate table using their
user_id. From that page they can enter different information that would be
saved in this separate database (for each user).
So the extra information they will be adding does not need to be added
right away, however to load the profile page with their display_name and
basic information I do need the information passed. Now that you bring it
up however, I suppose I could have the profile page just check if the
current user_id exists, and if yes then show the basic profile page with
just their display_name etc. But either way, once they enter information
into the on page form and submit it, the user-ID needs to be passed.
Which is where I came up with passing the current user ID, since they
would already be logged in and registered. Really most of the questions I
asked in this thread are variations of "how can I take the user_id and pass
it to a separate database upon registration (or upon form submit) so the
information in their specific table can be dumped back on their profile
page for viewing.
The information will (ideally) be outputted into a format where I could
format it with google charts or some type of visual display for them.
I hope that clarifies man, and again I am new to the loop so I apologize
if I am driving anyone insane here. Any advice welcome :)
Post by Gregory Lancaster
Post by Gregory Lancaster
When you say on the fly, you mean (for example) when a user registers,
have
Post by Gregory Lancaster
the user ID copied over into a sep database? That is actually what I am
trying to figure out haha. Id like to have the user->ID copied into a
separate database row *(or create a new table with the user-ID as the
table
Post by Gregory Lancaster
name)* after registration. If I could figure that out then I imagine the
rest would fall into place for me. The WP loop is something I am still
learning, so I really appreciate the reference information *Nicholas. *
*J.D* - Is that what you meant? Did I understand your question
correctly?
Post by Gregory Lancaster
if current_user->ID if exists fetch array($result) echo $row[1] etc...
Not exactly what I mean. My question was why are you copying the user_ID
over at that time - but that may not really be important.
If you need to add some information do the database when a user
registers, then you wan to use the user_register action hook as Nicholas
said.
http://codex.wordpress.org/Plugin_API/Action_Reference/user_register
But in your function hooked to that hook, you do *not* want to use the ID
of the current user. You need to use the ID of the user that has just
registered, which will be passed as the first parameter to your hooked
function, as shown in the examples there.
Back to my question, though, I'm not sure why you feel you need to do
this at the time the user registers. I assume that it has something to do
with the date/time oriented aspect of the data that you want to store?
I.e., I guess you have some entries that need to go in at that time so they
will be dated properly? But you are going to have to do this differently
for registered users...
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Post by Gregory Lancaster
When you say on the fly, you mean (for example) when a user registers,
have
Post by Gregory Lancaster
the user ID copied over into a sep database? That is actually what I am
trying to figure out haha. Id like to have the user->ID copied into a
separate database row *(or create a new table with the user-ID as the
table
Post by Gregory Lancaster
name)* after registration. If I could figure that out then I imagine the
rest would fall into place for me. The WP loop is something I am still
learning, so I really appreciate the reference information *Nicholas. *
*J.D* - Is that what you meant? Did I understand your question
correctly?
Post by Gregory Lancaster
if current_user->ID if exists fetch array($result) echo $row[1] etc...
Not exactly what I mean. My question was why are you copying the user_ID
over at that time - but that may not really be important.
If you need to add some information do the database when a user
registers, then you wan to use the user_register action hook as Nicholas
said.
http://codex.wordpress.org/Plugin_API/Action_Reference/user_register
But in your function hooked to that hook, you do *not* want to use the ID
of the current user. You need to use the ID of the user that has just
registered, which will be passed as the first parameter to your hooked
function, as shown in the examples there.
Back to my question, though, I'm not sure why you feel you need to do
this at the time the user registers. I assume that it has something to do
with the date/time oriented aspect of the data that you want to store?
I.e., I guess you have some entries that need to go in at that time so they
will be dated properly? But you are going to have to do this differently
for registered users...
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2013-08-26 19:42:43 UTC
Permalink
Now that you bring it up however, I suppose I could have the profile page just check if the
current user_id exists, and if yes then show the basic profile page with just their display_name etc.
Yes, I think that is what I would do.

You can get the ID of the current user in several different ways. You can use get_current_user_id() (http://codex.wordpress.org/Function_Reference/get_current_user_id) to get just the ID. If you want o get the WP_User object for the current user, then use wp_get_current_user() (http://codex.wordpress.org/Function_Reference/wp_get_current_user).
I should add, that the on page form used to submit this information is used
multiple times for dated information input. So if I use one table rather
than a table for each user_id then every row (and every button submit)
needs to grab the user-ID and input that with the form information so it
can be extracted. Thats why I was opting for table names prefixed with the
user-id rather than rows with a user ID column.
Yes, but one table with a user_id column just makes sense to me. You are going to need to grab the user ID on each submit either way. I definitely wouldn't want to clutter my database with hundreds of tables -- that's a bad idea, trust us on that one.
OH I understand what you're asking.
I want to pass the User_ID to the new table upon registering because I am
coding this for a profile page of sorts. So after registering, and
confirming registration they will load up say site.com/profile which will
dynamically pull their information from a separate table using their
user_id. From that page they can enter different information that would be
saved in this separate database (for each user).
So the extra information they will be adding does not need to be added
right away, however to load the profile page with their display_name and
basic information I do need the information passed. Now that you bring it
up however, I suppose I could have the profile page just check if the
current user_id exists, and if yes then show the basic profile page with
just their display_name etc. But either way, once they enter information
into the on page form and submit it, the user-ID needs to be passed.
Which is where I came up with passing the current user ID, since they would
already be logged in and registered. Really most of the questions I asked
in this thread are variations of "how can I take the user_id and pass it to
a separate database upon registration (or upon form submit) so the
information in their specific table can be dumped back on their profile
page for viewing.
The information will (ideally) be outputted into a format where I could
format it with google charts or some type of visual display for them.
I hope that clarifies man, and again I am new to the loop so I apologize if
I am driving anyone insane here. Any advice welcome :)
Gregory Lancaster
2013-08-26 19:55:44 UTC
Permalink
Thanks for your help guys - One last question. My form calls submit.php,
which is a separate file from wordpress obviously. I have tried using the
get_current_user_id and get_currentuserinfo() within submit with no
success. How can I get that information?
Post by Gregory Lancaster
Now that you bring it up however, I suppose I could have the profile
page just check if the
current user_id exists, and if yes then show the basic profile page with
just their display_name etc.
Yes, I think that is what I would do.
You can get the ID of the current user in several different ways. You can
use get_current_user_id() (
http://codex.wordpress.org/Function_Reference/get_current_user_id) to get
just the ID. If you want o get the WP_User object for the current user,
then use wp_get_current_user() (
http://codex.wordpress.org/Function_Reference/wp_get_current_user).
I should add, that the on page form used to submit this information is
used
multiple times for dated information input. So if I use one table rather
than a table for each user_id then every row (and every button submit)
needs to grab the user-ID and input that with the form information so it
can be extracted. Thats why I was opting for table names prefixed with
the
user-id rather than rows with a user ID column.
Yes, but one table with a user_id column just makes sense to me. You are
going to need to grab the user ID on each submit either way. I definitely
wouldn't want to clutter my database with hundreds of tables -- that's a
bad idea, trust us on that one.
OH I understand what you're asking.
I want to pass the User_ID to the new table upon registering because I am
coding this for a profile page of sorts. So after registering, and
confirming registration they will load up say site.com/profile which
will
dynamically pull their information from a separate table using their
user_id. From that page they can enter different information that would
be
saved in this separate database (for each user).
So the extra information they will be adding does not need to be added
right away, however to load the profile page with their display_name and
basic information I do need the information passed. Now that you bring
it
up however, I suppose I could have the profile page just check if the
current user_id exists, and if yes then show the basic profile page with
just their display_name etc. But either way, once they enter information
into the on page form and submit it, the user-ID needs to be passed.
Which is where I came up with passing the current user ID, since they
would
already be logged in and registered. Really most of the questions I asked
in this thread are variations of "how can I take the user_id and pass it
to
a separate database upon registration (or upon form submit) so the
information in their specific table can be dumped back on their profile
page for viewing.
The information will (ideally) be outputted into a format where I could
format it with google charts or some type of visual display for them.
I hope that clarifies man, and again I am new to the loop so I apologize
if
I am driving anyone insane here. Any advice welcome :)
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2013-08-26 20:02:58 UTC
Permalink
Post by Gregory Lancaster
Thanks for your help guys - One last question. My form calls submit.php,
which is a separate file from wordpress obviously. I have tried using the
get_current_user_id and get_currentuserinfo() within submit with no
success. How can I get that information?
You should probably change it so that rather than using an external file, the form submits to the same page within WordPress. Then check if the form is being submitted at the top of the page before displaying the form, and if it has, then save the submitted values (or maybe just include submit.php). Otherwise you will need to manually load WordPress from within your external file.
Gregory Lancaster
2013-08-26 21:22:18 UTC
Permalink
I got it about 30 mins ago, thanks so much for your help guys.

Continue reading on narkive:
Search results for 'Creating / Inserting into Table named after WP Username' (Questions and Answers)
5
replies
How do I send a Wordpress blog to a Flippa buyer?
started 2011-10-28 15:15:42 UTC
business & finance
Loading...