简体   繁体   中英

Inserting a Variable Into SQL Table

I cannot seem to get user entered data into a table and then printed.

Here is my code thus far:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
function submit() {
        var input = document.getElementById("save_name").value;
        localStorage.setItem("inputed_name", input);
        var msg;
        db.transaction(function (tx) 
        {
            tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log varchar(50))');
            tx.executeSql('delete from LOGS'); // Clears table (for debugging)

            tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, localStorage.inputed_name)');
            msg = '<p>Log message created and row inserted.</p>';
            document.querySelector('#status').innerHTML = msg;
        });

        db.transaction(function (tx) {
            tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
                var len = results.rows.length, i;
                msg = "<p>Found rows: " + len + "</p>";
                document.querySelector('#status').innerHTML += msg;
                for (i = 0; i < len; i++) {
                    msg = "<p><b>" + results.rows.item(i).log + "</b></p>";
                    document.querySelector('#status').innerHTML += msg;
                }
            }, null);
        });
}
</script>
</head>
<body>
<div id="status" name="status">Status Message</div>
<p>Enter data: <input type="text" id="save_name" name="inputed_name" /><br/></p>
<p><button onclick="submit()" type="button">Submit Data</button></p> <!--Should print data from the table-->
</body>
</html>

I got the "template"/start for this from here

Solved Below

Now I want to add two variables to my table, the original log variable and a second to act as a time stamp

function submit() 
{
    var input = document.getElementById("save_name").value;
    var msg;
    var time_stamp = new Date();

    db.transaction(function (tx) 
    {
        tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id auto_increment, date_time varchar(128), log varchar(64))');
        tx.executeSql('INSERT INTO LOGS (log, date_time) VALUES (?, ?)', [input, time_stamp]); // <--Problem here!
        msg = '<p>Log message created and row inserted.</p>';
        document.querySelector('#status').innerHTML = msg;
    });
}

The rest of the code is the same as above. I would think this would work but when I test it nothing gets in the table.

I think you just need to change the way you're getting and inserting your values (also, you had a syntax error in the SQL on this line):

var input = document.getElementById('someId').value;

...
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, ?)', [input]);
...

I think you can forget about the localStorage variable altogether, unless you need to persist those values.

要将多个变量插入表中,请使用以下格式。

tx.executeSql('INSERT INTO CacheRequest (key,value) VALUES (?,?)',[variableKey,variableValue]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM