2017-10-09 141 views
-1

嗨我很难将值插入我的数据库。 我可以使用我用于注册页面的相同方法登录。 我已经尝试了很多不同的选择,但似乎没有任何工作。用烧瓶插入mysql表格

这里是我的Python代码:

从瓶进口瓶,请求render_template,url_for 进口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

你可以这样做:在查询

sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)" 
cursor.execute(sql, ('[email protected]', 'very-secret')) 

要特别注意" "和后台蜱``。您的查询似乎是错误的。有没有价值的占位符%s

希望有所帮助。