将变量插入SQL表 [英] Inserting a Variable Into SQL Table
问题描述
我似乎无法使用户输入的数据进入表格然后进行打印.
到目前为止,这是我的代码:
<!DOCTYPE HTML>< html>< head>< script type =文本/javascript">var db = openDatabase('mydb','1.0','Test DB',2 * 1024 * 1024);函数commit(){var input = document.getElementById("save_name").value;localStorage.setItem("inputed_name",输入);var msg;db.transaction(函数(TX){tx.executeSql('如果不存在日志则创建表(ID唯一,日志varchar(50))');tx.executeSql('从日志中删除');//清除表格(用于调试)tx.executeSql('INSERT INTO LOGS(id,log)VALUES(1,localStorage.inputed_name)');msg ='< p>创建日志消息并插入行.</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>找到的行:"+ len +"/p";document.querySelector('#status').innerHTML + = msg;对于(i = 0; i< len; i ++){msg ="p"和"b".+ results.rows.item(i).log +"/b"/p;document.querySelector('#status').innerHTML + = msg;}}, 空值);});}</script></head>< body>< div id =状态";名称=状态">状态消息</div>< p>输入数据:< input type =文本"id ="save_name";name ="inputed_name";/< br/>/p< p><按钮onclick ="submit()"type ="button"> Submit Data</button></p><!-应该从表中打印数据-></body></html>
我从此处获得了模板"/开始>
解决以下
现在,我想在表中添加两个变量,即原始日志变量和一个用作时间戳记的变量
功能Submit(){var input = document.getElementById("save_name").value;var msg;var time_stamp = new Date();db.transaction(函数(TX){tx.executeSql('如果不存在日志则创建表(id auto_increment,date_time varchar(128),log varchar(64))');tx.executeSql('INSERT INTO LOGS(log,date_time)VALUES(?,?)',[input,time_stamp]);//<-问题在这里!msg ='< p>创建日志消息并插入行.</p>';;document.querySelector('#status').innerHTML = msg;});}
其余代码与上面相同.我认为这可以解决问题,但是当我进行测试时,表格中什么也没有.
我认为您只需要更改获取和插入值的方式即可(另外,此行的SQL中存在语法错误):
var输入= document.getElementById('someId').value;...tx.executeSql('INSERT INTO LOGS(id,log)VALUES(1,?)',[输入]);...
我认为您可以完全忘记 localStorage
变量,除非您需要保留这些值.
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.
这篇关于将变量插入SQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!