Jump to content

if one insert query fails, 'undo' the previous


paddy_fields
Go to solution Solved by Barand,

Recommended Posts

I realise 'undo' is the wrong term for this and I'm pretty sure it can't be done... but it's worth asking :)

 

When inserting a new user their details are put in different tables, a 'members' table with their login, a 'candidatePool' with other info, and a 'cvPool' with the the location of their cv. (a candidate doesn't need to be a member to be in the candidatePool, and one user can have many cv's so it can't all be in the same table)

 

When i insert the user into the member table i take the insert_id() and use that for the foreign key data for the next insert.

 

the problem I'm thinking is, what if the first query executes but the second fails? I would want the first query to then effectively delete what it's just inserted.

 

Is the solution to put a delete query in the 'failed query' part of the second, which will then delete the row from members according to the insert_id(), if that makes sense?

/* insert into members table */
if ($stmt = $db->prepare("INSERT INTO members (email, password, salt) VALUES (?, ?, ?)")) {
$stmt->bind_param('sss', $email, $password, $random_salt);
if (!$stmt->execute()) {
    header('Location: ../error.php?err=Member was not inserted');
    exit();
}
$member_id = $db->insert_id;
$stmt->close();
            
            
/* insert into candidatePool table */
$stmt = $db->prepare("INSERT INTO candidatePool (firstName,lastName,email,telephone,registered,members_id) VALUES (?,?,?,?,?,?)");
$stmt->bind_param('sssssi', $firstName, $lastName, $email, $telephone, $registered, $member_id);
if (!$stmt->execute()) {
    
   //SHOULD I PUT A DELETE QUERY HERE, TO DELETE THE RECORD I'VE JUST INSERTED INTO MEMBERS??

 
   header('Location: ../error.php?err=Candidate was not inserted');
   exit();

}
$candidate_id = $db->insert_id;
$stmt->close();

Edited by paddyfields
Link to comment
Share on other sites

I've tried to implement the ROLLBACK and COMMIT as suggested... and it appears to work. Well, it still creates both entries into each table and throws no errors at least.

 

Is there a way I can test the rollback?

$db->autocommit(FALSE); 

        /* insert into members table */ 
        if ($stmt = $db->prepare("INSERT INTO members (email, password, salt) VALUES (?, ?, ?)")) { 
        $stmt->bind_param('sss', $email, $password, $random_salt); 
        if (!$stmt->execute()) { 
            header('Location: ../error.php?err=Member was not inserted'); 
            exit(); 
        } 
        $member_id = $db->insert_id; 
        $stmt->close(); 
                     
                     
        /* insert into candidatePool table */ 
        $stmt = $db->prepare("INSERT INTO candidatePool (firstName,lastName,email,telephone,registered,members_id) VALUES (?,?,?,?,?,?)"); 
        $stmt->bind_param('sssssi', $firstName, $lastName, $email, $telephone, $registered, $member_id); 
        if (!$stmt->execute()) { 
           $db->rollback();  
           header('Location: ../error.php?err=Candidate was not inserted'); 
           exit(); 
        } 
        $candidate_id = $db->insert_id; 
        $stmt->close();
         
        $db->commit();
        $db->autocommit(TRUE);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.