SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm currently working on a MySQL database in phpMyAdmin.
    I would like to add some kind of cascade-functionality.

    Could someone help me with coding for the following problem?

    I have two tables:
    'Address' and 'User'. I have these two apart because a User can have more than one Address. The two tables both have the Key 'UserID'... to link them together.

    Because MySQL doesn't have a standard foreign-key function.. could someone help me with would I should do to link these two tables together.

    All help will be appreciated!

    Jazz

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to maintain the relationship yourself. So say you wanted to delete a user from table User. You would have to write two sql statements; one to delete the user from User and one two delete the related records in Address. So you could do something in PHP like this: (assumes that there is a db connection already established)
    PHP Code:

    function deleteUser($id) {
       
    $global $dbconnection;
       if(!
    $dbconnection) {
          return 
    0// return false
       
    }
       
    $sql "DELETE FROM User WHERE UserID = $id";
       
    $result mysql_query($sql);
       if(!
    $result) {
          return 
    0// return false
       
    }
       
    $sql "DELETE FROM Address WHERE UserID=$id";
       
    $result mysql_query($sql);
       return 
    $result;

    This function will return non-zero (true) if the deletion occured or 0 (false) if an error occured.


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
  •