SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict Avido's Avatar
    Join Date
    Jul 2003
    Location
    Kortrijk, Belgium, Europe, the world
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Transactions, the right way?

    Hello,

    a client of me want's a webapp. where users can input data at the same time (data that is placed in several tables, referential integrity is important here, or how do you say that ).

    So, when a query fails, the others that ran before, has to be cancelled. I believe, transactions is the right way? Right?

    Is the following code right? or am i missing the point?
    I put all the queries that are needed for 'one data insertion' in an array.

    Code:
    $_SESSION["n"] = $_POST["naam"];
    $aantal = $error = $done = 0;
     
    $sql = array();
     
    $sql[] = "START TRANSACTION;";
    $sql[] = "INSERT INTO tbl_test1(naam, moment) VALUES('" . $_SESSION["n"] . "', NOW());";
    $sql[] = "INSERT INTO tbl_test2(naam, inserted, moment) VALUES('" . $_SESSION["n"] . "', LAST_INSERT_ID(), NOW());";
     
    while(!$done && !$error)
    
    
    {
    $result = $db->query($sql[$aantal]); if($result->isError()){ $error=1; } $aantal++; if($aantal>=count($sql)){ $done = 1; }
    } if($error) { $sql = "ROLLBACK;"; echo "An error occurred, insertion is cancelled."; } else { $sql = "COMMIT;"; } $result = $db->query($sql);

  2. #2
    SitePoint Zealot newspire's Avatar
    Join Date
    Mar 2005
    Location
    Houston, TX
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


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
  •