Jump to content

linking tables


average american

Recommended Posts

Just to let you know i am new to this PHP stuff

 

So here it goes

 

I need to find a way to get my tables(listings, images) to link together.  I have an ID column in my listings table and a LISTING_ID column in my images table the prolbem is i cannot get the id from the listings table to the listing_id in the images table

 

any help is greatly appreciated.

 

by the way here is a part of the code:

 

mysql_query("LOCK TABLES listings, images WRITE");

 

mysql_query("INSERT INTO images (name, size, type, content ) ".

"VALUES ('$fileName', '$fileSize', '$fileType', '$content')");

$result = mysql_query("SELECT MAX(ID) AS LAST_ID FROM listings");

 

$id = mysql_fetch_array($result);

mysql_query("UNLOCK TABLES");

 

echo "Last ID#" . $id[LAST_ID];

echo "<br>File $fileName uploaded<br>";

 

Link to comment
Share on other sites

Hi,

 

Assuming the ID column in your listings table has been set up to be auto increment, you can use the mysql_insert_id() function to return the value of the id from the insert into the listings table, then pass that value into the listing_id column when inserting into your images table.

 

Hope this helps,

 

Darren.

Link to comment
Share on other sites

Hi,

 

Assuming the ID column in your listings table has been set up to be auto increment, you can use the mysql_insert_id() function to return the value of the id from the insert into the listings table, then pass that value into the listing_id column when inserting into your images table.

 

Hope this helps,

 

Darren.

 

And then you don't need to LOCK/UNLOCK the tables.

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.