Jump to content

[SOLVED] small problem, anyone?


justAnoob

Recommended Posts

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')";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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());
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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());
?>

Link to comment
Share on other sites

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());
?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.