Jump to content

php mysql rollback the transaction when user abort the connection


madura31

Recommended Posts

InnoDB supports rollback but I think you should do all the checks prior and have the file before you do any insertions.

 

this is my code.... InnoDB already okay.

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';

$database = 'csv_import';
$table = 'USER';

$con = mysqli_connect($db_host, $db_user, $db_pwd);
$mysqli = new mysqli($db_host, $db_user, $db_pwd, $database);

if (!$con)
    die("Can't connect db");

if (!mysqli_select_db($con, $database))
    die("can't select db");

if (isset($_POST['submit'])) {
    $fname = $_FILES['sel_file']['name'];
    echo 'upload file name : ' . $fname . ' <br />';
    $chk_ext = explode(".", $fname);

    if (strtolower(end($chk_ext)) == "csv") {
        $filename = $_FILES['sel_file']['tmp_name'];
        $handle = fopen($filename, "r");

        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

            $sql = "INSERT into USER(name,email,phone) values('$data[0]','$data[1]','$data[2]')";
            //mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
            $mysqli->autocommit(FALSE);
            $mysqli->query($sql);
            $mysqli->rollback();
            $mysqli->commit();

//            try {
//                $sql = "INSERT into USER(name,email,phone) values('$data[0]','$data[1]','$data[2]')";
//                //mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
//                $mysqli->autocommit(FALSE);
//                $mysqli->query($sql);               
//                $mysqli->rollback(); 
//            } catch (Exception $e) {                
//                echo $exc->getTraceAsString();
//            }
//            $mysqli->commit();
            //mysqli_query($con, $sql) or ( mysql_query("ROLLBACK") and die(mysqli_error($con) . " - $sql"));
        }


        echo "<br /><br />Success";
        fclose($handle);
    } else {
        echo 'invalid file';
    }
}
?>

<form action="<?php $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
    File - <input type="file" name="sel_file" size="20">
    <input type="submit" name="submit" value="submit">
</form>

Edited by madura31
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.