通过 html 和 python 将数据上传到数据库

Uploading data to a database through html and python

对于学校,我正在尝试构建一个基本表单以将数据上传到数据库。我正在通过 Flask 执行此操作,但在提交数据方面遇到了问题。填写表格并提交后,我返回错误 400 错误请求(浏览器(或代理)发送了该服务器无法理解的请求。)。以下是相关代码,如有任何建议,我们将不胜感激。

HTML:

<form action="http://127.0.0.1:5000/add" method="post">
            <div class="mb-3">
                <label class="form-label">Firstname</label>
                <input type="text" class="form-control" id="firstname" name="name" aria-describedby="emailHelp"
                       placeholder="Charlie" disabled>
            </div>
            <div class="mb-3">
                <label class="form-label">Surname</label>
                <input type="text" class="form-control" id="surname" name="name" aria-describedby="emailHelp"
                       placeholder="Nielsen" disabled>
            </div>
            <div class="mb-3">
                <label class="form-label">Date</label>
                <input type="date" class="form-control" id="date" name="date">
            </div>
            <div class="mb-3">
                <label class="form-label">Supervisor</label>
                <input type="text" class="form-control" id="supervisor" name="supervisor">
            </div>
            <div class="mb-3">
                <label class="form-label">Supervisor Number</label>
                <input type="number" class="form-control" id="supervisor_num" name="supervisor_num">
            </div>
            <div class="mb-3">
                <label class="form-label">Time Worked</label>
                <input type="number" class="form-control" id="unconfirmed_hours" name="unconfirmed_hours">
            </div>
            <div class="mb-3">
                <label class="form-label">Service Description</label>
                <textarea name="service_desc" rows="5" cols="55" maxlength='255'></textarea>

            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>

Python:

@app.route('/add', methods=['GET', 'POST'])
def add_service():
if request.method == 'GET':
    return render_template('add_service.html')
else:
    service_details = (
        request.form['firstname'],
        request.form['surname'],
        request.form['date'],
        request.form['supervisor'],
        request.form['supervisor_num'],
        request.form['unconfirmed_hours'],
        request.form['service_desc']
    )
    print(service_details)
    insert_service(service_details)
    return render_template('success.html')

def insert_service(service_details):
connie = sqlite3.connect(db_locale)
c = connie.cursor()
sql_execute_string_a = 'UPDATE student_details SET unconfirmed_hours = unconfirmed_hours WHERE firstname = firstname AND surname = surname'
sql_execute_string_b = 'INSERT INTO service_details (firstname, surname, date, supervisor, supervisor_num, unconfirmed_hours, service_desc) VALUES(?, ?, ?, ?, ?, ?, ?)'
c.execute(sql_execute_string_a, sql_execute_string_b, service_details)
connie.commit()
connie.close()

移除输入上的禁用。


   <div class="mb-3">
                <label class="form-label">Firstname</label>
                <input type="text" class="form-control" id="firstname" name="firstname" aria-describedby="emailHelp"placeholder="Charlie" hidden>
            </div>
            
            
            <div class="mb-3">
                <label class="form-label">Surname</label>
                <input type="text" class="form-control" id="surname" name="surname" aria-describedby="emailHelp"
                       placeholder="Nielsen" >
            </div>

也更改他们的名字匹配 request.form