我正在创建一个网站,管理员用户可以在该网站中动态创建自定义测验,并将其设置为一组用户。用户可以根据需要添加任意数量的问题,然后在正确答案字段旁边选择一个单选值。
我尝试将数据库中的值名称作为数组输入,并通过数组中的项目数实现列,但是没有运气向数据库提交任何内容,但是它表示无法连接到本地服务器。
如何根据函数Add()的调用次数创建相应的列数,并在列中使用位类型将正确答案(通过无线电选择)设置为true?
<form action="" method="POST">
<fieldset id="input1-wrapper">
<legend>Quiz Storage</legend>
<p>Year: <input type="text" name="year" placeholder="Year" value="{{request.form.year}}"><br /></p>
<p>Topic: <input type="text" name="topic" placeholder="Topic" value="{{request.form.topic}}"><br /></p>
<p>Title: <input type="text" name="title" placeholder="Title" value="{{request.form.title}}"><br /></p>
</fieldset>
<fieldset id="fieldset">
<legend id="legend">Questions</legend>
<div id="placeholder">
<div id="template">
<p>Question: <br><input type ="text" size="80" name="ques[]" value="{{request.form.question }}"/><br /></p>
<p>Answer 1: <br><input type ="text" size="80" name="ans1[]" value="{{request.form.answer1 }}"/><input type="radio" value="true"><br /></p>
<p>Answer 2: <br><input type ="text" size="80" name="ans2[]" value="{{request.form.answer2 }}"/><input type="radio" value="true"><br /></p>
<p>Answer 3: <br><input type ="text" size="80" name="ans3[]" value="{{request.form.answer3 }}"/><input type="radio" value="true"><br /></p>
</div> <!-- template -->
</div> <!-- placeholder -->
<p><button type="button" name="Submit" onclick="Add();">Add new item</button></p>
</fieldset>
</form>
<p><button type="button" name="Submit">Submit</button></p>
<script>
var _counter = 0;
function Add() {
_counter++;
var oClone = document.getElementById("template").cloneNode(true);
oClone.id += (_counter + "");
document.getElementById("placeholder").appendChild(oClone);
}
</script>
SQLite代码:(python)
#Insert quiz titles into SQLite database
def register_quiz():
try:
print("register_quiz")
db=sqlite3.connect(dbname)
print(sqlite3.version)
db.row_factory=sqlite3.Row
cursor = db.cursor()
print("Topic = " + request.form[topic'])
cursor.execute('''INSERT INTO quizzes (Year, Topic, Title) VALUES (?,?,?)''', (request.form['first_name'],request.form['last_name'], request.form['passwordID']))
db.commit()
except Error as e:
print(e)
finally:
db.close()
#Store
def register_questions():
try:
print("register_quiz")
db=sqlite3.connect(dbname)
print(sqlite3.version)
db.row_factory=sqlite3.Row
cursor = db.cursor()
print("Topic = " + request.form[topic'])
cursor.execute('''INSERT INTO questions (Title, Question, Answer1, Answer2, Answer3) VALUES (?,?,?,?,?)''', (request.form['first_name'],request.form['last_name'], request.form['passwordID'], request.form['email_ID'], request.form['year']))
db.commit()
except Error as e:
print(e)
finally:
db.close()