您好我在将值插入数据库时遇到了困难。 我可以使用与注册页面相同的方法登录。 我尝试过很多不同的选择,但似乎没什么用。
这是我的python代码:
来自flask导入Flask,request,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)
和我的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>
答案 0 :(得分:0)
您可以执行以下操作:
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ('abc@python.org', 'very-secret'))
在查询中特别注意" "
和后退``
。您的查询似乎有问题。占位符%s
没有值。
希望有所帮助。