Pulling up var from another table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Pulling up var from another table

Post by 4Boredom »

I am creating an articles section on my website, where any user can submit one. I need to code it so that I know the user # that submitted it.. So I need:

Code: Select all

$userid = $usernumber;
However, Userid is in the basic table which was not in tho form submitted on the previous page. And on the registration form for the other page I just set that to auto_increment, where here it shouldn't.

How do I pull up $userid from a table named 'basic'.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

upping this as it has been over the 24 hour time frame I have to wait to do so.
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post by ryuuka »

if the user name is in the table where the usernumbers are located you can use something like this:

Code: Select all

select usernumber, username
from basic_table
where (username = '$userid')
or you could use join to compare the username to the userID

goodluck
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

no... they are in different tables....

I need to pull userid from basic... and save it as usernumber on a dif table?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

In the articles table, create a column that stores a copy of the auto-incrementing ID from the users table. When the article is submitted, insert the user's ID into the articles table along with the rest of the article information. When you are displaying the articles and you also want to display the user's information, write a function that fetches the username from the users table with an ID that matches the copy in the articles table.

Code: Select all

# if $userid is the user ID stored in the articles table:
SELECT username FROM users WHERE id = '$userid';
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

Writing a copy wont work here.

One user should be able to submit multiple articles.

Thers not a way I can just call up $userid and then make $usernumber a copy of that data (while processing article)?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please show your table definitions in more detail (e.g. as sql dump)
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

Code: Select all

$query = "SELECT userid FROM basic"; 
$result = mysql_query($query) or die ($ERROR_MSG . mysql_error());
$userid = $usernumber;
This still posts the UserId as 0...


and as far as table definitions...

$userid is in the table 'basic'.. thats where I need to call from
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

nevermind... the select function worked

I needed to switch Usernumber and userid in the last line

WORKS NOW!

Thank you to all who responded!
Post Reply