SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Enthusiast MAC#1's Avatar
    Join Date
    Mar 2003
    Location
    ontario
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question joining of field from one table to another

    JOINING? Would it be a join that I would use to get the 'cust_id' (auto-incremented from Mysql) from table CUSTOMER into the other tables of CARD PAYMENT & CUST ORDER. If so, how? Also, does this work even if they are queried at the same time?



    // database connection
    mysql_connect("$host","$user","$pass" );
    // database selection
    mysql_select_db($database);

    // Insert customer information
    mysql_query("INSERT INTO customer VALUES(
    '$cust_id',
    '$posted',
    '$firstname',
    '$lastname',
    '$company',
    etc...

    // Insert payment
    mysql_query("INSERT INTO card_payment VALUES(
    '$posted',
    '$cust_id',
    '$cart_id',
    '$total')" );

    // Insert customer order
    mysql_query("INSERT INTO cust_order VALUES(
    '$posted',
    '$cust_id',
    '$cart_id')" );

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    Do you want the id which MySQL creates when a new record is added?

    You can get it with PHP using mysql_insert_id()
    http://www.php.net/manual/en/functio...-insert-id.php

    This can then be put in another table

    PHP Code:
    mysql_query('INSERT INTO ... ') or die(mysql_error());

    // get the auto_incremented id
    $id mysql_insert_id();

    // insert it into another table
    mysql_query("INSERT INTO sometable VALUES ($id)") or die(mysql_error()); 
    I added the "or die" part just in case to see if any errors occur.

    Christian


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
  •