-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
51 lines (37 loc) · 1.28 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from flask import Flask, request, send_from_directory, redirect, url_for
import mysql.connector
app = Flask(__name__)
@app.route('/')
def index():
return send_from_directory('.', 'index.html')
@app.route('/css/<path:path>')
def send_css(path):
return send_from_directory('css', path)
@app.route('/images/<path:path>')
def send_images(path):
return send_from_directory('images', path)
@app.route('/submit', methods=['POST'])
def submit():
name = request.form['name']
email = request.form['email']
message = request.form['message']
# Save the data to the database
save_to_db(name, email, message)
return f"Form submitted successfully! Name: {name}, Email: {email}, Message: {message}"
def save_to_db(name, email, message):
# Connect to MySQL database
conn = mysql.connector.connect(
host="localhost",
user="sarvesh",
password="root123",
database="contacts" # Replace with your actual database name
)
cursor = conn.cursor()
# Insert form data into the database
cursor.execute("INSERT INTO submissions (name, email, message) VALUES (%s, %s, %s)",
(name, email, message))
conn.commit()
cursor.close()
conn.close()
if __name__ == '__main__':
app.run(debug=True)