2017-10-09 3 views
-1

안녕하세요. 데이터베이스에 값을 삽입하는 데 어려움이 있습니다. 가입 페이지에서 사용하는 것과 같은 방법으로 로그인 할 수 있습니다. 많은 옵션을 시도했지만 아무 것도 작동하지 않는 것 같습니다.플라스크로 mysql 테이블에 넣기

플라스크 가져 오기 플라스크, 요청,는 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에 대한 값은 없습니다.

희망이 있습니다.

관련 문제