php - Trying to use beginTarnsaction() and commit() but it fails when I put the commands in -
i'm little confused why doesn't work, far can tell, i've done nothing different various other examples i've seen.
if run following code;
try{ //set connection $db = new pdo($dbcon, $user, $pass); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); //start transaction $db->begintransation(); //sql insert basic info $sql1 = //insert statement here $query1 = $db->prepare($sql1); $query1->execute(array(...)); //run sql commands above $db->commit(); //set success message $return['message'] = 'success'; } catch(exception $e) { $db->rollback(); $return['message'] = "error: ".$e; }; //$return = $_post; $return["json"] = json_encode($return); echo json_encode($return);
then update fails. if run same command without begin/commit lines;
try{ //set connection $db = new pdo($dbcon, $user, $pass); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); //sql insert basic info $sql1 = //insert statement here $query1 = $db->prepare($sql1); $query1->execute(...)); //set success message $return['message'] = 'success'; } catch(exception $e) { $db->rollback(); $return['message'] = "error: ".$e; }; //$return = $_post; $return["json"] = json_encode($return); echo json_encode($return);
then insert works fine.
i've seen various exmaples (mostly on site, though various other sites), have $db = new pdo...
, $db->setattrib...
lines outside of try catch block (which i've tried , didn't help).
other that, far can see, in code lines other peoples examples, apparently work.
i'm new pdo's, , first time i've every tried use begintransation()
, commit()
.
i should point out in example,i i've added single update, in reality there 5 update statements on 5 different tables, dependant on previous table update working. why want use begin/commit, believe can use $db->rollback();
in catch undo in case of error (at least how understand it??)
i'd if point out mistakes i'm making (or @ least point me in right direction), i'm sure obvious , i'm missing basic!
thanks in advance.
edit i've edited codes above, remove information don't think other people needed see (things database connection details, , exact insert statements being used).
i found more examples, , after lot of messing around, have got block of code works (note i've removed actual insert statements in code below).
i don't know changed, since i'm sure i'd tried layout before, i'm willing assume there typo in code originally, i've managed correct in various trials.
the full code layout (with actual statements removed stated above) was;
//set connection $db = new pdo($dbcon, $user, $pass); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); try{ //start transaction $db->begintransaction(); //sql insert basic info $sql1 = //insert statement here $query1 = $db->prepare($sql1); $query1->execute(); //with parameters //set member_id variable $member_id = $db->lastinsertid(); //sql insert address info $sql2 = // $query2 = $db->prepare($sql2); $query2->execute(); //with parameters //sql insert medical info $sql3 = $query3 = $db->prepare($sql3); $query3->execute(); //with parameters //sql insert ethnicity , disability info $sql4 = $query4 = $db->prepare($sql4); $query4->execute(); //with parameters //sql insert declaration info $sql5 = $query5 = $db->prepare($sql5); $query5->execute(); //with parameters //run sql commands above $db->commit(); //set success message $return['message'] = 'database updated correctly'; }catch (pdoexception $exception){ $db->rollback(); $return['message'] = 'fail: '.$exception; }; //$return = $_post; $return["json"] = json_encode($return); echo json_encode($return);
interestingly rollback();
doesn't work in example, if can't figure 1 out, question. (incidentally, if figure out why rollback();
doesn't work, i'll update answer future visitors)!
Comments
Post a Comment