justAnoob Posted March 25, 2009 Share Posted March 25, 2009 i have 2 tables. table 1 id name table 2 pic_id picture descrip my question is how do i get the id from table 1 to be the same number as the pic_id in table 2. The only reason i ask is because a user may have more than one picture of him or herself. i tried something like this, but obviously im not retrieving the id from table one correctly. I am new at php so go easy on me. <?php $pic_id = mysql_insert_id(); $sql = "INSERT INTO table2(name)VALUES('$name',', '$pic_id')"; Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/ Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 Have your first table as it is and have the second table like this: id personid picture That way each person can have as many pics as they want all pointing to the user by personid Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793587 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 still a little confused. sorry Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793591 Share on other sites More sharing options...
revraz Posted March 25, 2009 Share Posted March 25, 2009 In your second table, your first ID is the Row ID, and since that is unique, each person can have multiple pictures. Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793597 Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 table: users id name id: this is the unique identifier for this table and will be numeric table: pictures id userid description id: this is the unique identifier for this table and will be numeric userid: this will contain a number that references the "user" table allowing and endless number of pictures to be associated with a person. Examples... users id=1 name=Joe Bloggs id=2 name=Peter Pan pictures id=1 userid=1 id=2 userid=1 id=3 userid=2 The first pictures (IDs 1 & 2) belong to Joe Blogs because the userid is set to 1 The last picture (ID 3) belongs to Peter Pan because userid is set to 2 Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793602 Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 Sorry, I didn't see your code at the bottom there - I thought that was part of your signature! $pic_id = mysql_insert_id(); $sql = "INSERT INTO table2 (name) VALUES ('$name',', '$pic_id')"; Two problems spring to me there. 1. mysql_insert_id() is only really userful after you've INSERT'd. If you need to know the id number of an existing record you'd need to run a SELECT query first and use the data from it 2. In your INSERT query you've specified one field for insert (name) but you've specified two ($name,$picid) they need to be balanced Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793605 Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 Here's some more... Let's say we already know the userid of the person we want to add a picture to (it's Peter Pan who has the ID of 2) mysql_query("INSERT INTO pictures (`userid`,`description`) VALUES ('2','Underexposed')"); Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793608 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 thanks for the help,, here is the scoop again... a user will be logged in, that user will not know there user id number(just for the admin) so after they are signed in, they can upload pictures, as many as they want, then they can click on a button on the page that will only show there pictures. So I think what you explained to me should help. Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793616 Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 Any more problems don't hesitate to come back! Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793618 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 i guess where im stuck now is selecting the user id from table 1 so it can be used in table 2 for each pic that the logged in user has uploaded. maybe i'm thinking this too hard. so what would the best way to go about this be. sorry for all the qeustions. do you have a small example code so i could learn this. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793626 Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 Let's say we have a name - Peter Pan and we want his userid. $result=mysql_query("SELECT * FROM users WHERE `name`='Peter Pan'"); $row=mysql_fetch_assoc($result); echo 'userid: '.$row['id'].'<br />'; echo 'name: '.$row['name']; Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793633 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 thanks for the reply bud. Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793635 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 now i put this into another little project that I'm working on, and the problem is that the id of the user from the table 'members' is not getting inserted into the user id field of the table member_trades... what i'm trying to say is how do i get the id from the user that is logged in? i tried using $findit variable which is actually a sesssion variable that is created when the user logs in... i am confused,,, again. Yesideez I know your out there somewhere,, lol. <?php $_SESSION['id'] = "$username"; //this is actually set when the user logs in, just wanted to post here to show $findit=$_SESSION['id']; $result=mysql_query("SELECT id FROM members WHERE 'username'= '$findit'"); //******* $user_id=mysql_fetch_assoc($result); $sql = "INSERT INTO member_trades(item_name, description, in_return, user_id)VALUES('$item_name','$description','$in_return', '$user_id')"; mysql_query($sql) or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793715 Share on other sites More sharing options...
revraz Posted March 25, 2009 Share Posted March 25, 2009 $user_id would be an array, so you need to specify $user_id['id'] Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793730 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 not sure what your getting at, sorry Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793736 Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 mysql_fetch_assoc() returns an array so in your next INSERT query you need to use this: $sql = "INSERT INTO member_trades(item_name, description, in_return, user_id)VALUES('$item_name','$description','$in_return', '$user_id['id']')"; That's because you assigned $user_id the result from the query: $user_id=mysql_fetch_assoc($result); What you could do is use this: $row=mysql_fetch_assoc($result); Which makes it all more readable. Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793751 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 this is what i have so far, i'm not even sure if i'm getting the id from the table correctly using the $findit Does that look right?? <?php $_SESSION['id'] = "$username"; $findit=$_SESSION['id']; $result=mysql_query("SELECT id FROM members WHERE 'username'= '$findit'"); $row=mysql_fetch_assoc($result); $sql = "INSERT INTO member_trades(item_name, description, in_return, user_id)VALUES('$item_name','$description','$in_return', '$row')"; mysql_query($sql) or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793768 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 I am so lost now,,,, not sure what to do next...all i want to do is get the id for a member from one table and put it in another table when they upload a pic so i know what pictures go to each user. i know i am able to do this. what is going on? <?php $result=mysql_query("SELECT id FROM members WHERE username = 'WHAT GOES HERE. MUST BE A VARIABLE FOR WHATEVER USER IS LOGGED IN RIGHT???'"); $row=mysql_fetch_assoc($result); $user_id = $row; $sql = "INSERT INTO member_trades(item_name, description, in_return, user_id)VALUES('$item_name','$description','$in_return', '$user_id')"; mysql_query($sql) or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793796 Share on other sites More sharing options...
MadTechie Posted March 25, 2009 Share Posted March 25, 2009 $user_id = $row; should be $user_id = $row['id']; Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793802 Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 Stick with it - it's soooooo rewarding when it finally works! Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793804 Share on other sites More sharing options...
MadTechie Posted March 25, 2009 Share Posted March 25, 2009 Stick with it - it's soooooo rewarding when it finally works! Since when has ripping your hair out been "rewarding" JK (just kidding) But its true it does get easier with time! Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793807 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 thanks guys,, i'll give it a shot... i never like to give up. Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793808 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 when the user logs in,,, this happens <?php $_COOKIE['user'] = "$username"; ?> I am storing the username as a cookie so i can use it for this,, the upload part. <?php $findit=$_COOKIE['user']; $result=mysql_query("SELECT id FROM members WHERE 'username'= '$findit'"); $row=mysql_fetch_assoc($result); $user_id = $row['id']; $sql = "INSERT INTO member_trades(item_name, description, in_return, user_id)VALUES('$item_name','$description','$in_return', '$user_id')"; mysql_query($sql) or die(mysql_error()); ?> but still nothing,,,, a number 0 still shows up in the member_trades table for my user_id Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793811 Share on other sites More sharing options...
revraz Posted March 25, 2009 Share Posted March 25, 2009 You are not setting the cookie correctly. http://us3.php.net/setcookie You probably should use a session instead. Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793825 Share on other sites More sharing options...
justAnoob Posted March 25, 2009 Author Share Posted March 25, 2009 is there a better way to get the username of whoever is logged in?>?? Quote Link to comment https://forums.phpfreaks.com/topic/151064-solved-small-problem-anyone/#findComment-793833 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.