Delete rows of local database as each is transfered to hosted database
EDIT - got the transfer to happen
.. now I need to figure out how to get the successful transfers to delete each row after it is stored on the host. The function that grabs the data from the browsers SQL database is :
function grabLocal(){
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM entries', [], function(tx, results){
var len = results.rows.length, i;
for (i = 0; i < len; i++) {
var qID = (results.rows.item(i).ID);
var qEmail = (results.rows.item(i).email);
var qPhone = (results.rows.item(i).phone);
var qMonth = (results.rows.item(i).birth_month);
var qDay = (results.rows.item(i).birth_day);
var qYear = (results.rows.item(i).birth_year);
var qMonth = (results.rows.item(i).birth_month);
var qGender = (results.rows.item(i).gender);
var qContest = (results.rows.item(i).contest);
var qStore = (results.rows.item(i).storeNo);
var qDate = (results.rows.item(i).added_on);
var localData = new Array((qEmail),(qPhone),(qMonth),(qDay),(qYear),(qGender),(qContest),(qStore),(qDate));
$.ajax({
type: "POST",
url: "sendIt.php",
data: "email=" + qEmail + "&phone=" + qPhone + "&month=" + qMonth + "&day=" + qDay + "&year=" + qYear + "&gender=" + qGender + "&contest=" + qContest + "&store=" + qStore + "&date=" + qDate,
success: function(){
// delete each row as passed to host
db.transaction(function(tx)
{
tx.executeSql('DELETE FROM entries WHERE ID = ?', [qID]);
});
// alert ("Success");
}
});
}
});
});
}
the php is simple like :
<?php
$link = mysql_connect('host', 'user', 'password');
if (!$link) {
die('Could not connect: ' .mysql_error());
}
echo 'Connected successfully';
$email = htmlspecialchars(trim($_POST['email']));
$phone = htmlspecialchars(trim($_POST['phone']));
$month = htmlspecialchars(trim($_POST['month']));
$day = htmlspecialchars(trim($_POST['day']));
$year = htmlspecialchars(trim($_POST['year']));
$gender = htmlspecialchars(trim($_POST['gender']));
$contest = htmlspecialchars(trim($_POST['contest']));
$store = htmlspecialchars(trim($_POST['store']));
$datestamp = htmlspecialchars(trim($_POST['date']));
mysql_select_db("sandbox_info") or die (mysql_error());
mysql_query("INSERT INTO `infoData` (email, phone, birth_month, birth_day, birth_year, gender, storeNo, contest, added_on) VALUES ('$email','$phone','$month','$day','$year','$gender','$store','$contest','$datestamp')");
echo 'Data uploaded';
mysql_close($link);
?>
Can someone please help explain what is wrong with the delete transaction? It does delete one row each time I run the process but I would like it to delete all of them. I guess I could drop the whole table but I would rather it runs a check after each transfer to the remote host and then deletes the local data. I would also appreciate any tips on how this code could just be improved in general. Thanks in advance for any advice!
REVELATION
OK i echoed back the qID in the success and found out since it is deleting out each row it is then giving开发者_JS百科 a new id to the next available row and keeps echoing out the same id over and over. So... I know the problem but still need some help to find the fix.
You seem to have described it well enough: if you already have HTTP POST support in your app (using java.net.URL or HttpClient) and a PHP script that works on the server, you'll need to:
- Detect when someone is online (look at their IP address or try and connect to your server).
- Generate a list of all unsaved transactions: you've got the basic code for that above, maybe you need some flag.
- For each row, generate a POST to the server: if the POST succeeds (returns a 200 success code) then delete the row from the local database (or mark it as synchronized in some other way).
That should do it?
I'm not 100% clear on what you are trying to do but...
You could have one side generate a list of INSERT statements from the data that would be executed on the other side.
精彩评论