SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: slight problem

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    slight problem

    I have two tables which are linked to one another through id and download_id.

    I've set up an add download page which once filled up and submitted processes the form results and sends the results to table a.

    Table a has say the following fields:
    id
    name
    alias
    size
    date added
    features

    Table b on the other hand has the below:
    id
    download_id
    version
    size
    date added
    changes

    Table a has the usual bits and pieces of info you'd get on a download. Table b would boast a record of each version of the download.

    What I want is after it inserts the data to table a; for it to find the id # of the download I just added and to use that as the download_id in my second mysql insert statement.

    PHP Code:
    $query2 "SELECT id,alias FROM sections_downloads WHERE alias = $alias";
          
    $result2 mysql_query($query2);
          while (
    $download mysql_fetch_array($result))
                  {
                  
    "$download_id = $download[id]";
            } 
    Above code didn't seem to work at all. Any ideas guys?
    Bardius

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After your first insert use.


    PHP Code:
    $result mysql_query("INSERT ....");
    $download_id mysql_insert_id(); 
    Now $downlaod_id holds the id from the last record inserted. Consequently the reason why your first attempt didn't work is cause you forgot to use single quotes around $alias
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You definitely are the coolest cat in town
    Bardius

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok have jumped into another similar problem.

    What if I were to insert data to table b first and then want to update one field in table a but would need to retrieve the dowload_id first.

    Remember these two tables are linked via id in table a and download_id in table b. The download_id is not a unique and not auto incremented.
    Bardius

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this what you mean?

    PHP Code:
    $did mysql_insert_id();
    $result mysql_query("SELECT download_id FROM table b WHERE id = '$did'");
    extract(mysql_fetch_array($result));

    /* $download_id now contains the value of the "download_id" column in the record you just interested. */ 

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are inserting a download_id into table B first. Then the download_id must be stored in some var, cause you just inserted it. There should be no reason that I can see as to why you would need to requery the database to get data you just used in an INSERT.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I thought at first, Freddy, but I'm really not sure. He may have mis-worded his problem a bit.

  8. #8
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry guys but I think I got myself confused while thinking about all this quite late at night after hard day's work which judging by the looks of things had the same effect on you.
    Bardius


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •