Home > Back-end >  Uploading data to a database through html and python
Uploading data to a database through html and python

Time:04-16

For school, I am attempting to construct a basic form to upload data to a database. I am doing this through Flask and I am having issues regarding submitting the data. After filling out the form and submitting I am returned with the error 400 bad request (The browser (or proxy) sent a request that this server could not understand.). Below is the relevant code any suggestions would be appreciated.

HTML:

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

            </div>
            <button type="submit" >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()

CodePudding user response:

Remove the disabled on the inputs.


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

Also change their name matching the request.form

  • Related