Hallo Ich habe Schwierigkeiten, Werte in meine Datenbank einzufügen. Ich kann mich mit derselben Methode anmelden, die ich für die Anmeldeseite verwende. Ich habe viele verschiedene Optionen ausprobiert, aber nichts schien zu funktionieren.Einfügen in Mysql-Tabelle mit Kolben
Hier ist mein Python-Code:
von Kolben Import Flask, Anfrage, render_template, url_for Import pymysql.cursors
app = Flask(__name__)
# Connect to the database
connection = pymysql.connect(host='localhost',
user='root',
password='1234',
db='mydb',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
@app.route('/')
def index():
return render_template('signup.html')
@app.route('/signup', methods=['POST', 'GET' ])
def signup():
cursor = connection.cursor()
if request.method == 'POST':
try:
cursor.execute('''INSERT INTO users (user, fname, lname, email, pnum, password) VALUES (%s, %s, %s, %s, %s, %s)''')
cursor.commit()
finally:
return render_template('login.html')
if __name__ == '__main__':
app.run(debug=True)
und mein HTML:
<h1>Please Sign Up</h1>
<button onclick="document.getElementById('id01').style.display='block'"style="width:auto;">Sign Up</button>
<div id="id01" class="modal">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
<form class="modal-content animate" action="/signup" method="POST">
<div class="container">
<label><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" required>
<label><b>user</b></label>
<input type="text" placeholder="user" name="user" required>
<label><b>First name</b></label>
<input type="text" placeholder="First name" name="fname" required>
<label><b>pnum</b></label>
<input type="text" placeholder="Pnum" name="pnum" required>
<label><b>Last name</b></label>
<input type="text" placeholder="Last name" name="lname" required>
<input type="checkbox" checked="checked"> Remember me
<p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>
<div class="clearfix">
<button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
<button type="submit" action="/Signup" method="POST" class="signupbtn">Sign Up</button>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById('id01');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>