-1

i started learning flask just few days ago so i know this might not be the best code you've seen so far my main goal for now is just to get my data into the db. find my code below

from flask import Flask, session, redirect, url_for, escape, request, render_template
from hashlib import md5
from dbConnect import connection
import MySQLdb
from MySQLdb import escape_string as thwart
import gc
app = Flask(__name__)
#database conn
conn = MySQLdb.connect(host="localhost", user="root", password="jccofficial", db="test")
cur = conn.cursor()
#registration handling
@app.route('/register/', methods=["GET","POST"])
def register_page():

   return render_template('register.html')
   try:

     if request.method == "POST":
        username  = request.form['username']
        email = request.form['email']
        password = request.form['password']


        x = cur.execute("SELECT * FROM users WHERE username = (%s)",
                      (username))

        if int(x) > 0:
            flash("That username is already taken, please choose another")
            return render_template('register.html', form=form)

        else:
            cur.execute("INSERT INTO users (username,email,password,)VALUES (%s,%s,%s)", (username, email, password))

            conn.commit()
            flash("Thanks for registering!")
            cur.close()
            conn.close()                
            session['logged_in'] = True
            session['username'] = username

            return redirect(url_for('dashboard'))

      return render_template("register.html", form=form)
   except Exception as e:
    return(str(e))

I've setup and confirmed that my db is working by manually inserting data into it.However i do not know how to check that my form is actually posting the inputs.

  • You are opening your DB connection once globally, but you are closing it after the `INSERT`. This will work only once. – Klaus D. May 25 '18 at 02:30
  • @Klaus Thanks for your answer but it doesn't even work even for once. hitting submit button just reloads the register page. no error messages nothing. I'm just two days in and i know I'm getting a whole lot of things wrong – Onyenanu Princewill May 25 '18 at 19:23

1 Answers1

0

As Klaus D. pointed out in the comments you are opening your db connection globally then closing it in your function.

The other thing is in your register_page function you are immediately returning a template return render_template('register.html') this means all your other code in the function is unreachable. It will never run! You probably want to move that return or ensure it only runs based on some condition.

Finally I see that you are storing passwords in plaintext. This may serve you well for learning exercise, but its a great programming sin to store passwords in plaintext! See why should I hash passwords. Even simple hashing isn't that great. You usually want a strong key derivation function like hashlib.pbkdf2_hmac.

rsiemens
  • 615
  • 6
  • 15
  • Thanks for your answer, but I cant actually figure out where else to place the return statement as moving it yields a "name: 'form' not defined" error page. I'd appreciate if i can be provided with a working user registration snippet. like i mentioned I'm just two days in and haven't really gotten that hang on flask to help me debug correctly. As for the password hashing, thanks for the info but this is solely for learning purpose. – Onyenanu Princewill May 25 '18 at 19:38