Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过Flask应用程序向MySQL提交表单值,我在代码中做错了什么?_Python_Mysql_Flask - Fatal编程技术网

Python 通过Flask应用程序向MySQL提交表单值,我在代码中做错了什么?

Python 通过Flask应用程序向MySQL提交表单值,我在代码中做错了什么?,python,mysql,flask,Python,Mysql,Flask,我试图根据表单输入生成随机代码,比如用户想要生成的随机代码的数量。然后将这些数据添加到MySQL表中 以下是HTML: <HTML> <BODY bgcolor="cyan"> <form method="POST" action=""> <center> <H1>Enter your details </H1> <br> Code-Attribute (eg. itsa19) <i

我试图根据表单输入生成随机代码,比如用户想要生成的随机代码的数量。然后将这些数据添加到MySQL表中

以下是HTML:

<HTML>
<BODY bgcolor="cyan">
<form method="POST" action="">
    <center>
    <H1>Enter your details </H1> <br>
    Code-Attribute (eg. itsa19) <input type = "text" name= "code_attribute" /> <br>
    Max Usage <input type = "number" name = "max_usage" /> <br>
    Max No of Users <input type = "number" name = "users" /> <br>
    Data Volume (in GB) <input type = "number" name = "data_volume" /> <br>
    How many codes you want to create? <input type = "number" name = "number_of_codes" /> <br>
    <input type="submit" value="Upload code in Database">
    </center>
</form>
</BODY>
</HTML>
请帮忙。
提前谢谢。

它有效,只是一个小错误。NumberOfCodes在str中而不是int值。该缩进看起来是错误的。它显示
随机数()。
from flask import Flask, render_template, request
from flask_mysqldb import MySQL
app = Flask(__name__)


app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'MY_DB'

mysql = MySQL(app)


@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == "POST":  // Gets the form inputs
        details = request.form
        AttributeCode = details['code_attribute']
        MaxUsage = details['max_usage']
        NumberOfUsers= details['users']
        UsageVol = details['data_volume']  
        NumberOfCodes = details['number_of_codes']

    import string 
    import random

    def random_numbers():    // Generate Random Codes (Column 1)
            N = 7
            res = ''.join(random.choices(string.ascii_lowercase +
                             string.digits, k = N)) 

            prefix = AttributeCode
            DiscountCode = prefix + res
            columns = [MaxUsage, NumberOfUsers, UsageVol]
            randoms.append(DiscountCode)     // appends column values
            randoms.extend(columns)   
            cur = mysql.connection.cursor()
            cur.execute("INSERT INTO discount_codes(codes, max_usage, users, data_volume) VALUES (%s, %s, %s, %s)", (randoms[0],randoms[1],randoms[2],randoms[3]))  # I have also tried puting ('TestCode', 2 ,3 ,4)
            mysql.connection.commit()
            cur.close()

        for _ in range(NumberOfCodes):
            random_numbers()   
        return 'success'

    return render_template('index.html')


if __name__ == '__main__':
    app.run()