2

So I am using Flask to pass an input from html to a mysql db. However it only works for single characters ie can only input "5" and "a", however it cannot input "55" or "at". Here is my code.

__init__.py :
@app.route('/statistics',methods=['GET','POST'])
def statsinput():
        if request.method=='GET':
                return render_template("statsinput.html")
        else:
                first_name=(request.form['fname'])
                c, conn = connection()
                query="INSERT INTO test VALUES(%s)"
                c.execute(query,first_name)
                conn.commit()
                return ('working')

statsinput.html:
<!DOCTYPE html>
<html>
   <body>
    <form method="POST">
      First name: <input type="text" name="fname"><br>
      <input type="submit" value="Submit">
    </form>
  </body>
</html>

Here is the description for my mysql table:
mysql> DESCRIBE test;
 +--------+----------+------+-----+---------+-------+
| Field  | Type     | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| ticker | char(50) | YES  |     | NULL    |       |
+--------+----------+------+-----+---------+-------+

1 Answer 1

1

Insert correctly in the query string.

query="INSERT INTO test VALUES({})".format(first_name)
c.execute(query)
Sign up to request clarification or add additional context in comments.

2 Comments

@GuyMowbray What is doesn't work. This will work sure.
unfortunately not, dont know whats wrong with it; ive tried to follow instructions from this (stackoverflow.com/questions/42662502/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.