1

I have 5 inputs and one html table. I need to check the checkbox that are in the html table so I need to loop through the table and get the data. The 5 inputs and the data on html table are related so if one of the insert statement fails, this will be a problem.

So far validating the inputs and checking the html table checkbox are working, but what if one of my insert statement failed? That being said, I need to delete the data inserted if one fails.

This is the structure of my website:

  1. Validate the inputs
  2. Loop through html table where checkbox is checked
  3. AJAX code here calling insert
  4. if statement here to check if the checkbox is checked
  5. if yes then
    -> AJAX code here
    -> Call another insert statement
    else ->no data to be saved, because the inputs and html table value need each other

UPDATE:

if im going to use transaction how can i apply it? on my website im using two different php file for insert. one in the loop. and one for the 5 inputs this is my code

var checkedItems = $('#dataTable input[type="checkbox"]:checked').each(function() {
        $.ajax({
                    type: "POST",
                    url: "insertdocumentsignatory.php",
                    data: ({dtnum: tnum, dsignum: signum})
                })
                .done(function (msg) {
                    alert("Data Saved: " + msg);
                })
        .fail(function() {
            alert( "Posting failed." );
        }); 
        });
    });
    if (!checkedItems.size()) {
        alert ("Nothing checked");
        return;
    }else if (checkedItems.size()) {

        $.ajax({
                    type: "POST",
                    url: "insertdocument.php",
                    data: ({dtnum: tnum, ddoctitle: doctitle, ddoctype: doctype, ddoccontent: doccontent, ddocdatefilled: docdatefilled})
                })
                .done(function (msg) {
                    alert("Data Saved: " + msg);
                })
        .fail(function() {
            alert( "Posting failed." );
        });
    }
knowmeifyou
  • 217
  • 7
  • 17

3 Answers3

2

you can write insert queries in transaction. If any of the insertion fails then it can rollback.

Danish Ahmed
  • 171
  • 1
  • 9
2

You should be using PDO transactions. A sample transaction looks like this:

<?php
    $pdo->beginTransaction();
    $checkbox = 1; // suppose checkbox is checked
    if($checkbox) {
        $insert = $pdo->prepare("INSERT INTO anytable (anyrow) VALUES (?)");
        $insert->bindValue(1, "anyvalue");
        $insert->execute();
        if($insert->rowCount() > 0) {
            $insert2 = $pdo->prepare("INSERT INTO anytable2 (anyrow2) VALUES (?)");
            $insert2->bindValue(1, "anyvalue2");
            $insert2->execute();
            if($insert2->rowCount() > 0) {
                $pdo->commit();
            } else {
                $pdo->rollBack();
            }
        } else {
            $pdo->rollBack();
        }
    }
?>

Manual: http://php.net/manual/en/pdo.transactions.php

Rehmat
  • 4,681
  • 3
  • 22
  • 38
2

Transaction will ensure that, either happen all or nothing. Most of the leading Database management system support transaction feature. Since you are using PHP I found below link, which explains how to use transaction with PHP + MySql

PHP + MySQL transactions examples

Spider
  • 514
  • 1
  • 10
  • 22