I made a program to insert and update records in a sqlite database and to view data in a database in Websql.
I created a text box field, and when we enter the data that it stores in the database by clicking the submit button.
I could not get the records in the database to be updated with the new values entered in the text box.
However, I can insert data into the database and cannot update the values in the database.
Here is my coding.
var updateStatement = "UPDATE login SET username=?,password=?,firstname = ?, lastname = ? ,hobby=?,email=? WHERE id=?";
var db=window.openDatabase('MYFORM','1.0','MYFORM',200000);
$(document).ready(function()
{
initDatabase();
$("#update").click(updateRecord);
});
function initDatabase() // Function Call When Page is ready.
{
try {
if (!window.openDatabase)
{
alert('Databases are not supported in this browser.');
}
else {
showRecords();
}
}
catch (e) {
if (e == 2) {
console.log("Invalid database version.");
} else {
console.log("Unknown error " + e + ".");
}
return;
}
}
function updateRecord() // Get id of record . Function Call when Delete Button Click..
{
var usernamenew = $('input:text[id=username]').val().toString();
var passwordnew = $('input:password[id=pass]').val().toString();
var firstnamenew = $('input:text[id=firstname]').val().toString();
var lastnamenew = $('input:text[id=lastname]').val().toString();
var hobbynew = $('input:text[id=hobby]').val().toString();
var emailnew= $('input:text[id=email]').val().toString();
var useridnew = $("#id").val();
db.transaction(function (tx) { tx.executeSql(updateStatement, [usernamenew,passwordnew,firstnamenew, lastnamenew,hobbynew,emailnew,Number(useridnew)],loadAndReset, onError) });
}
HTML
<label for="username"> Username :</label>
<label for="pass"> Password :</label>
<input name="password" type="password" id="pass" placeholder="your password" size="20" maxlength="20" width="20" />
<label for="firstname"> Firstname :</label>
<input type="text" name="firstname" id="firstname" placeholder="your full name"/>
<label for="lastname"> Lastname :</label>
<input type="text" name="lastname" id="lastname" placeholder="your last name"/>
<label for="hobby"> Hobby : </label>
<input type="text" name="hobby" id="hobby" placeholder="your hobby" /><br ><br >
<label for="email"> Email :</label>
<input type="text" name="email" id="email" placeholder="Your email address"/><br >
<br >
<a href="#" class="ui-btn ui-btn-inline ui-corner-all ui-btn-b "id="update" type="submit" >Update</a>
UPDATE QUESTION
showRecords(), , , , , ..ie..
id = 1, loadRecords() id = 1 .
, plz.
showRecords
function showRecords() // Function For Retrive data from Database Display records as list
{
$("#results").html('')
db.transaction(function (tx) {
tx.executeSql(selectAllStatement, [], function (tx, result) {
dataset = result.rows;
for (var i = 0, item = null; i < dataset.length; i++) {
item = dataset.item(i);
var linkeditdelete = '<ul><li>' + item['username'] + ' , ' + item['password'] + ' ,' +item['firstname']+' , ' + item['lastname'] +' , ' + item['hobby']+','+item['email']+ '<a href="page4.html" onclick="loadRecord(' + 'id' + ') id="edit";">edit</a>' +' ' + '<a href="#" onclick="deleteRecord(' + item['id'] + ');">delete</a></li></ul>';
$("#results").append(linkeditdelete);
}
});
});
}
function loadRecord(i)
{
var item = dataset.item(i);
$("#username").val((item['username']).toString());
$("#pass").val((item['password']).toString());
$("#firstname").val((item['firstname']).toString());
$("#lastname").val((item['lastname']).toString());
$("#hobby").val((item['hobby'])).toString();
$("#email").val((item['email']).toString());
$("#id").val((item['id']).toString());
}