What am I missing? - PDO + transaction + multiple requests -

UPDATED - SEE THE ADDITION BELOW

OK, right now, I'm not a developer (maybe someday), I know almost nothing, classes, functions, methods, wrappers, foos, bars, and most of all OOP confuse forever-loving-s from me. That being said, I am sure that there are many things that I could do better and invite your criticism and knowledge. However...

My specific question is: I missed some vital condition for the interaction between the WSDL from which I receive data and PHP + PDO, MySQL combos, and everything will fall apart as soon as I click

The three tables in the code below should normalize a fairly large set of data obtained through the web-based customer service, this part is an automated process (cron job) that extracts code from 6 other files. I had to make some changes to the database to host a new client, and I decided that WTH, give PDO a try again. Only now I don’t feel that the code that I see for him is almost embarrassing, maybe he does it right (yes, I tested it many times many times today, multiple import, everything went without a hitch) I'm going to push the last update is someday tomorrow, and to be honest, I'm a little worried that I missed something serious and will go out with a bunch of corrupted data while I'm out of town this week. Excuse me,if this seems inconsequential, but I spent a lot of time on sites like these, and I know as little as I do, most of the information either suggests too much or is immersed in things that I am not yet competent in (see the list above).

- , , PDO ? , ? , ON DUPLICATE KEY UPDATE ..??

FYI: , . , , . - . 3 , table1 , 2 ..

<pre><code><?php

// connect to db Mysqli style
require_once 'MysqliCurrentLoginQuery.file'; //get the variables not supplied below

/*~~~~~~~~~~~~~~~~SET YOUR VARIABLES HERE~~~~~~~~~~~~~~~~~*/
/*A bunch of soap variables to be passed to MySOAPReq.file*/
/*~~~~~~~~~~~~~~~~SET YOUR VARIABLES HERE~~~~~~~~~~~~~~~~~*/

 require_once 'MySOAPRequest.file'; //This is where $soapresult is passed from

 //convert array to ph objects for use in prepared stmt
 $xmlResponse = new SimpleXMLElement($soapresult);
    foreach ($xmlResponse->xmlWorkOrders->workOrder as $order) {       
        try {
            require 'MyPDOdbConfigAndConnection.file'; //where $conn is passed from
            $conn->beginTransaction();
                    try {
                        $query1 = "INSERT INTO table(`id`,`b`,`c`,`d`) 
                            VALUES ('" . "','". 1 . "','". 1 . "','". $order->D . "')
                            ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id),d=$order->D";
                        $result1 = $conn->prepare($query1);
                        $result1->execute();
                        $lastid = $conn->lastInsertID();
                    } catch(PDOExecption $e) {
                        $conn->rollback();
                        print "Error!: " . $e->getMessage() . "</br>";
                    }
                    try {                                
                        $query2 = "INSERT INTO table2(`id`, `f`, `g`) 
                            VALUES ('" . "','" . $order->F . "','" . $lastid . "')
                            ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), f=$order->F";
                        $result2 = $conn->prepare($query2);
                        $result2->execute();
                        $lastid = $conn->lastInsertID();
                        print "<br />" . $conn->lastInsertID() . "<br />";
                    } catch(PDOExecption $e) {
                        $conn->rollback();
                        print "Error!: " . $e->getMessage() . "</br>";
                    }
                    try {
                        $dnsdateparts=explode(' ',$order->H);
                        $query3 = "INSERT INTO table3(`id`, `g`, `h`, `i`) 
                            VALUES ('" . "','" . $order->G . "', STR_TO_DATE('" . $dateparts[0] . "','%m/%d/%Y'),'" . $dateparts[1] . "')
                            ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), g=G, h=H, i=$lastid";
                        $result3 = $conn->prepare($query3);
                        $result3->execute();
                        $conn->commit();                        
                    } catch(PDOExecption $e) {
                        $conn->rollback();
                        print "Error!: " . $e->getMessage() . "</br>";

                    }                   
        } catch( PDOExecption $e ) {
            print "Error!: " . $e->getMessage() . "</br>";                 
        }                     
    }
?><code><pre>

, . 1 3 , , ! , !

, - , - - , , , ( , , , ).

!!!

! X - ' CRS'Road. , , ! .

- , . , , , , . !

<pre><code>
<?php

// I actually got some functions to work!!
// So everything necesary self-requires from one required file
require_once ('/path/to/this/file/some.functions.php');


/*~~~~~~~~~~~~~~~~SET YOUR VARIABLES HERE~~~~~~~~~~~~~~~~~*/
/*A bunch of soap variables to be passed to MySOAPReq.file*/
/*~~~~~~~~~~~~~~~~SET YOUR VARIABLES HERE~~~~~~~~~~~~~~~~~*/

// Uses function from some.functions.php
$soapresult = mySoapClientMaker($avariable, $anothervariable);


 //convert array to ph objects for use in prepared stmt
 $xmlResponse = new SimpleXMLElement($soapresult);
    foreach ($xmlResponse->xmlWorkOrders->workOrder as $order) {       
        try {
            //$conn is now already there from some.functions.php
            $conn->beginTransaction();
                    try {
                        // Create the query string and store it in a variable
                        $query1 = "INSERT INTO table(`id`,`b`,`c`,`d`) "
                            . "VALUES (:col1, :col2, :col3, :col4)"
                            . "ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id),d=" . $order->D;

                        // Prepare the query i.e. assemble the pieces of the string 
                        $result1 = $conn->prepare($query1);

                        // Bind Values/Params
                        // PDO will not properly escape everything in the inserts without this
                        // This was the source of the broken import, lesson learned
                        $result1 ->bindValue(':col1', NULL, PDO::PARAM_NULL);
                        $result1 ->bindValue(':col2', 1, PDO::PARAM_INT);
                        $result1 ->bindValue(':col3', 1, PDO::PARAM_INT);
                        $result1 ->bindValue(':col4', $order->D, PDO::PARAM_STR);

                        // Execute (still in try mode) the now prepared/escaped query
                        $result1->execute();

                        // Remember the primary key from this insert to use as
                        // the foreign key in the next insert
                        $lastid = $conn->lastInsertID();

                    } catch(PDOExecption $e) {
                        // If your insert breaks, here everything
                        // goes back to its pre-insert state. 
                        $conn->rollback();
                        print "Error!: " . $e->getMessage() . "</br>";
                    }
                    // Repeat as above
                    try {                                
                        $query2 = "INSERT INTO table2(`id`, `f`, `g`) "
                            . "VALUES (:col1, :col2, :col3) "
                            . "ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), f=" . $order->F;

                        $result2 = $conn->prepare($query2);

                        $result2 ->bindValue(':col1', NULL, PDO::PARAM_NULL);
                        $result2 ->bindValue(':col2', 1, PDO::PARAM_INT);   
                        $result2 ->bindValue(':col3', $order->D, PDO::PARAM_INT);

                        $result2->execute();
                        $lastid = $conn->lastInsertID();

                    } catch(PDOExecption $e) {
                        $conn->rollback();
                        print "Error!: " . $e->getMessage() . "</br>";
                    }
                    // Repeat as above again
                    try {
                        $dateparts=explode(' ',$order->H);
                        $query3 = "INSERT INTO table3(`id`, `g`, `h`, `i`) "
                            . "VALUES (:col1, :col2, :col3, :col4) "
                            . "ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), g=G, h=H, i=" . $lastid;

                            VALUES ('" . "','" . $order->G . "', STR_TO_DATE('" . $dateparts[0] . "','%m/%d/%Y'),'" . $dateparts[1] . "')                           
                        $result3 = $conn->prepare($query3);

                        $result3 ->bindValue(':col1', NULL, PDO::PARAM_NULL);
                        $result3 ->bindValue(':col2', $order->G, PDO::PARAM_INT);
                        $result3 ->bindValue(':col3', "STR_TO_DATE(" . $dnsdateparts[0] . "','%m/%d/%Y')", PDO::PARAM_STR);
                        $result3 ->bindValue(':col4', $dateparts[1], PDO::PARAM_STR);

                        $result3->execute();

                        // NOW if everything made it this far without error
                        // it will all be committed to the db
                        $conn->commit();

                    } catch(PDOExecption $e) {
                        $conn->rollback();
                        print "Error!: " . $e->getMessage() . "</br>";
                    }                   
        } catch( PDOExecption $e ) {
            print "Error!: " . $e->getMessage() . "</br>";                 
        }                     
    }
?>
<code><pre>

P.S. , - , . PDO

+3

All Articles