[thelist] MySQL CREATE TABLE - img file??

Ben Phillips ben at inchima.com
Tue May 14 09:57:00 CDT 2002

>   I prefer the referencing method.  And I have a couple of questions
>   about that!!  :o)
>   How does the img name get controlled!!  - I mean how does the name
>   of the img get changed to be the same as the primary key.

well, as your primary key for 'courses' is set to auto_increment, you
don't know what it will be when you create a new record. if you are
editing a record, you can simply retrieve the primary key for that
record and use that to name the image.

however, it depends on what language you are using (if it's php then i
can probably help) and how you are uploading the image to the server.

if you are using php and you are using a web form to create the new
record, then you can insert the record, use the mysql_insert_id() php
function to obtain the primary key of the freshly inserted record, and
then move the uploaded file to your photos directory, naming it after
the primary key.

sample code (image uploaded into $image)

    // create new courses record
    $query  = "insert into courses set ...";
    mysql_query($query, $db);

    // this gets the auto_increment primary key of the most recent
    $imageid = mysql_insert_id();

    // save image to photos directory
    $image_location =
    move_uploaded_file($image, $image_location);

this assumes that your image file is a gif, and various other things,
but you should be able to see from that how it basically works.

if you are using php then:
can help you with handling file uploads.


More information about the thelist mailing list