【问题标题】:Inserting into mysql table with flask [duplicate]用烧瓶插入mysql表[重复]
【发布时间】:2017-10-09 17:57:57
【问题描述】:

您好,我在将值插入数据库时​​遇到了困难。 我可以使用与注册页面相同的方法登录。 我尝试了很多不同的选项,但似乎没有任何效果。

这是我的python代码:

from flask import Flask, request, 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>

【问题讨论】:

    标签: python html css mysql flask


    【解决方案1】:

    你可以这样做:

    sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
    cursor.execute(sql, ('abc@python.org', 'very-secret'))
    

    特别注意查询中的" " 和反引号``。您的查询似乎有问题。占位符 %s 没有值。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-30
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      相关资源
      最近更新 更多