从表单提交将数据写入mySQL数据库

问题描述 投票:0回答:1

我正在尝试将数据写入表单提交到 mySQL,但它不会出现在我的数据库中。

我正在学习编程,如果我没有提供足够的信息,请原谅我。

表单提交中的数据是用户注册时的电子邮件和密码。

以下是注册表单代码:

<!--This is for the Modal sign up-->
      <h2>Sign Up Form</h2>
      <form id="signupFormJS" class="SignUpFormCSS" method="post">
        <div class="input-field">
          <input type="email" id="email" name="email" class="emailCSS" placeholder="Enter your email" required>
        </div>
        <div class="input-field">
          <input type="password" id="password" name="password" class="passwordCSS" placeholder="Enter your password" required>
        </div>
          <button class="SubmitButtonCSS" id="SubmitButtonJS" type="submit">SignUp</button>

以下是将数据写入 mySQL 数据库的 python 文件中的代码:

@app.route('/signup', methods=['GET', 'POST']) 
def signup():
   SignUpForm1 = SignUpFormStructure()
   if SignUpForm1.validate_on_submit():
      email = request.form.get['email']
      password = request.form.get['password']
      username = None  # Assuming you're intentionally setting this to None
        db.session.add(record)
        db.session.commit()
      SQL_Location = "INSERT INTO user (password, username, email) VALUES (%s, %s, %s)"
      cursor.execute(SQL_Location, (hashed_password, username, email))
      db.commit()

我已经能够将数据从Python写入我的数据库,所以我很满意我使用了正确的路径、端口等。但是,当我尝试从表单写入数据时,它不起作用。

总之,我正在使用 HTML、JavaScript、Python(带有 Flask 表单和 SQL 炼金术)

我已经尝试了上述方法,我希望将数据写入数据库,但这不起作用。

python flask sqlalchemy
1个回答
0
投票

这并不是在模板中表示 WTForms 的正确方法...... 请让我们看看你的

SignupFormStructure() class

© www.soinside.com 2019 - 2024. All rights reserved.