Home > Database > Mysql Tutorial > How to develop a simple online questionnaire using MySQL and Python

How to develop a simple online questionnaire using MySQL and Python

WBOY
Release: 2023-09-20 11:18:35
Original
867 people have browsed it

How to develop a simple online questionnaire using MySQL and Python

How to use MySQL and Python to develop a simple online questionnaire

Introduction
Online questionnaires are widely used in modern society to collect user information Views, feedback and comments. This article will introduce how to use MySQL and Python to develop a simple online questionnaire system, and provide relevant code examples.

1. Database design

  1. Create a database named survey:

    CREATE DATABASE survey;
    Copy after login
  2. Create a database named questions and responses Table:
    Create questions table, used to store questionnaire questions:

    CREATE TABLE questions (
     id INT PRIMARY KEY AUTO_INCREMENT,
     question_text VARCHAR(255) NOT NULL
    );
    Copy after login

    Create responses table, used to store user answers:

    CREATE TABLE responses (
     id INT PRIMARY KEY AUTO_INCREMENT,
     question_id INT,
     response_text VARCHAR(255) NOT NULL,
     FOREIGN KEY (question_id) REFERENCES questions(id)
    );
    Copy after login

2. Python code implementation

  1. Import the necessary libraries:

    import mysql.connector
    from mysql.connector import Error
    from flask import Flask, request, render_template
    Copy after login
  2. Connect to the MySQL database:

    def create_connection():
     connection = None
     try:
         connection = mysql.connector.connect(
             host='localhost',
             database='survey',
             user='your_username',
             password='your_password'
         )
         if connection.is_connected():
             print('Connected to MySQL database')
     except Error as e:
         print(e)
    
     return connection
    Copy after login
  3. Create Flask application and routing:

    app = Flask(__name__)
    
    @app.route('/')
    def home():
     # 获取所有的问题
     connection = create_connection()
     cursor = connection.cursor()
     query = 'SELECT * FROM questions'
     cursor.execute(query)
     questions = cursor.fetchall()
     cursor.close()
     connection.close()
    
     return render_template('index.html', questions=questions)
    
    @app.route('/survey', methods=['POST'])
    def survey():
     # 获取用户的答案
     connection = create_connection()
     cursor = connection.cursor()
     response_text = request.form['response']
     question_id = request.form['question_id']
     query = 'INSERT INTO responses (question_id, response_text) VALUES (%s, %s)'
     cursor.execute(query, (question_id, response_text))
     connection.commit()
     cursor.close()
     connection.close()
    
     return 'Thank you for your response!'
    
    if __name__ == '__main__':
     app.run()
    Copy after login
  4. Create front-end page (index.html):

    <!DOCTYPE html>
    <html>
    <head>
     <title>Survey</title>
    </head>
    <body>
     <h1>Survey</h1>
     <form action="/survey" method="POST">
         {% for question in questions %}
             <p>{{ question[1] }}</p>
             <input type="hidden" name="question_id" value="{{ question[0] }}">
             <input type="text" name="response" required>
         {% endfor %}
         <input type="submit" value="Submit">
     </form>
    </body>
    </html>
    Copy after login

3. Run and test

  1. Run the Python code in the terminal:

    python survey_app.py
    Copy after login
  2. Visit http://localhost:5000 in the browser to see the questionnaire page.

Conclusion
This article introduces how to use MySQL and Python to develop a simple online questionnaire system. Through database design and related code implementation, users' opinions, feedback and opinions can be easily collected. By enriching the design and functionality of the front-end page, the user experience of the questionnaire can be further improved. I hope this article can be helpful to readers and inspire more innovative ideas.

The above is the detailed content of How to develop a simple online questionnaire using MySQL and Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template