phong_ux Posted June 4, 2013 Share Posted June 4, 2013 (edited) <?php/** * This is an example code that shows how you can save Handsontable data on server using PHP with PDO (SQLite). * This code is not intended to be maximally efficient nor safe. It is for demonstrational purposes only. * Changes and more examples in different languages are welcome. * * Copyright 2012, Marcin Warpechowski * Licensed under the MIT license. * http://warpech.github.com/jquery-handsontable/ */try { //open the database $db = new PDO('sqlite:cars.sqlite'); //will create the file in current directory. Current directory must be writable //create the database if does not exist $db->exec("CREATE TABLE IF NOT EXISTS cars (id INTEGER PRIMARY KEY, manufacturer TEXT, year INTEGER, price INTEGER)"); $colMap = array( 0 => 'manufacturer', 1 => 'year', 2 => 'price' ); if ($_POST['changes']) { foreach ($_POST['changes'] as $change) { $rowId = $change[0] + 1; $colId = $change[1]; $newVal = $change[3]; if (!isset($colMap[$colId])) { echo "\n spadam"; continue; } $select = $db->prepare('SELECT id FROM cars WHERE id=? LIMIT 1'); $select->execute(array( $rowId )); if ($row = $select->fetch()) { $query = $db->prepare('UPDATE cars SET `' . $colMap[$colId] . '` = :newVal WHERE id = :id'); } else { $query = $db->prepare('INSERT INTO cars (id, `' . $colMap[$colId] . '`) VALUES(:id, :newVal)'); } $query->bindValue(':id', $rowId, PDO::PARAM_INT); $query->bindValue(':newVal', $newVal, PDO::PARAM_STR); $query->execute(); } } elseif ($_POST['data']) { $select = $db->prepare('DELETE FROM cars'); $select->execute(); for ($r = 0, $rlen = count($_POST['data']); $r < $rlen; $r++) { $rowId = $r + 1; for ($c = 0, $clen = count($_POST['data'][$r]); $c < $clen; $c++) { if (!isset($colMap[$c])) { continue; } $newVal = $_POST['data'][$r][$c]; $select = $db->prepare('SELECT id FROM cars WHERE id=? LIMIT 1'); $select->execute(array( $rowId )); if ($row = $select->fetch()) { $query = $db->prepare('UPDATE cars SET `' . $colMap[$c] . '` = :newVal WHERE id = :id'); } else { $query = $db->prepare('INSERT INTO cars (id, `' . $colMap[$c] . '`) VALUES(:id, :newVal)'); } $query->bindValue(':id', $rowId, PDO::PARAM_INT); $query->bindValue(':newVal', $newVal, PDO::PARAM_STR); $query->execute(); } } } $out = array( 'result' => 'ok' ); echo json_encode($out); // close the database connection $db = NULL;}catch (PDOException $e) { print 'Exception : ' . $e->getMessage();}?> PLEASE HELP!!Thank You!! Edited June 4, 2013 by phong_ux Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.