SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member lplatz's Avatar
    Join Date
    Feb 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    2 Table, 1-Many, 2 input forms

    Goal. For each client, they have a minimum of 1 pet, though upwards of n pets.
    Two tables: client & pets; Two forms: client form & pet form. The parent table/form is the client. Complete & submitting the Client form will navigate to the Add a Pet form. When the pet form is complete the user has the option to end or add additional pets. Knowing the level of knowledge here, I'm here as a novice to learn with no need to over-design this simple database application.

    1st off, having read through Kevin Yanks 4th edition Database Driven Web Site (great book / reference), I didn't find any examples that fit the application model noted above. I did, however, learn how to set up the tables and much of the PHP involved with getting the form data into the table.

    My hurdle is discovering how to use/transfer the mysql_insert_id() from the client query to the pets query in order to set up the one-to-many relationship for the eventual join queries.

    links to examples would be greatly welcomed as I can't imagine this is anything new.

    My apologies for the run-on request.

  2. #2
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its pretty easy, so you have your query:

    PHP Code:
    // we need to start the session if we are accessing $_SESSION
    session_start();

    $query mysql_query("INSERT INTO clients (a, b) VALUES ('$a', '$b')"); 
    $newID mysql_insert_id(); 

    // at this stage I would store the client id into the session to remember it for the pet form
    $_SESSION['client_id'] = $newID
    Now for the pet form
    PHP Code:
    // we need to start the session if we are accessing $_SESSION
    session_start();

    $client_id = (int)$_SESSION['client_id'];
    $query mysql_query("INSERT INTO pets (client_id, b) VALUES ($client_id, '$b')"); 
    This is probably the easiest it can be, perhaps the next level would be to allow user logins to retrieve and edit their pets in the future.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  3. #3
    SitePoint Member lplatz's Avatar
    Join Date
    Feb 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_sessions - well I am anxious to play around with this and will follow with the outcome. Many thanks!

  4. #4
    SitePoint Member lplatz's Avatar
    Join Date
    Feb 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked brilliantly! Set within two controller pages (thanks Kevin Yank for that great direction) the data flow to the two tables worked perfectly. This has set me up to begin working on the join query.


Tags for this Thread

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
  •