how to connect html register form to mysql database with python

To connect an HTML registration form to a MySQL database using Python, you can use the Flask web framework and the PyMySQL library. Here is an example of how you can do this:

1. Install Flask and PyMySQL by running the following command:

pip install flask pymysql

2. Create a new Python file and import Flask and PyMySQL:

from flask import Flask, request

import pymysql


3. Create a new Flask application and connect to the MySQL database:

app = Flask(__name__)


connection = pymysql.connect(







4. Create a new route in your Flask application that will handle the form submission:

@app.route(‘/register’, methods=[‘POST’])
def register():
    # Get form data
    name = request.form[‘name’]
    email = request.form[’email’]
    password = request.form[‘password’]
    # Insert data into MySQL
    with connection.cursor() as cursor:
        sql = “INSERT INTO users (name, email, password) VALUES (%s, %s, %s)”
        cursor.execute(sql, (name, email, password))
    return ‘User registered successfully!’

5. In the HTML form, when the submit button is clicked, the form’s data should be sent to the ‘/register’ route using the HTTP POST method.

<form action=”/register” method=”post”>

  <input type=”text” name=”name” placeholder=”Name”>

  <input type=”email” name=”email” placeholder=”Email”>

  <input type=”password” name=”password” placeholder=”Password”>

  <input type=”submit” value=”Register”>



6. Finally, run the Flask application by calling

if __name__ == ‘__main__’:


Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *