Posts: 198
Threads: 17
Joined: Jun 2020
Hey guys I'm not getting any errors but my username and password are always added to the table as NULL. Anyone know why?
Thanks!
Python: https://pastebin.com/efvtFYDK
HTML: https://pastebin.com/b3HzpEsv
Terminal: https://www.picpasteplus.com/v.php?i=aed4900e61
Posts: 12,031
Threads: 485
Joined: Sep 2016
Please post code in code tags and not use links, Thank you
I lot of folks will skip if they have to follow a link
Posts: 198
Threads: 17
Joined: Jun 2020
Jun-09-2020, 02:27 AM
(This post was last modified: Jun-09-2020, 03:08 AM by card51shor.)
import os
import psycopg2
from flask import Flask, session, render_template, request
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
app = Flask(__name__)
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
@app.route("/")
def index():
return "Project 1: TODO"
try:
connection = psycopg2.connect(user = "zpztmsdffwqvs",
password = "16671705ed2adsfd7855f36cb8c05a5188abc7fd823c850e46625",
host = "ec2-54-33-37-321.compute-1.amazonaws.com",
port = "5432",
database = "dafdafsdasd")
cursor = connection.cursor()
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "GET":
return render_template("register.html")
elif request.method == "POST":
password = request.form.get("password")
username = request.form.get("username")
print (username)
print (password)
# session.clear()
# db.execute("INSERT INTO users (username, password) VALUES (:username, :password)", {"username":username, "password":password})
# db.commit()
return render_template("register.html", message=username)
should i start a new thread?
Posts: 198
Threads: 17
Joined: Jun 2020
I'm not sure why it's putting NULL for username and NULL for password instead of the user input. Can someone please help me?
Thanks.
import os
import psycopg2
from flask import Flask, session, render_template, request
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
app = Flask(__name__)
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
@app.route("/")
def index():
return "Project 1: TODO"
try:
connection = psycopg2.connect(user = "zpztmsgfdceigfwqvs",
password = "fsgfdfg",
host = "ec2-54-55-37-115.compute-1.amazonaws.com",
port = "5432",
database = "sdgfd")
cursor = connection.cursor()
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "GET":
return render_template("register.html")
elif request.method == "POST":
password = request.form.get("password")
username = request.form.get("username")
session.clear()
db.execute("INSERT INTO users (username, password) VALUES (:username, :password)", {"username":username, "password":password})
db.commit()
return render_template("register.html", message=username)
Posts: 1,838
Threads: 2
Joined: Apr 2017
Jun-09-2020, 04:14 AM
(This post was last modified: Jun-09-2020, 04:17 AM by ndc85430.)
Are you sure you're using the correct syntax for the placeholders in the statement? The docs suggest that what you're doing is incorrect.
Also, a couple of things:
1
You shouldn't leave your DB connection details in your post.
2. Is this application for your own learning or something meant for serious use? If the latter, you shouldn't be storing passwords in the clear; they should be hashed and salted.
Posts: 198
Threads: 17
Joined: Jun 2020
it's just for learning and i changed the database info don't worry.
Also - I think this is OK for placeholders I am getting no syntax errors.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Jun-09-2020, 05:22 AM
(This post was last modified: Jun-09-2020, 05:22 AM by buran.)
(Jun-09-2020, 02:27 AM)card51shor Wrote: should i start a new thread? Generally you shouldn't have started these 2 threads that I merged. There is your original one where we were iterating over the syntax of the SQL statement. This time I will not merge this into the original one, because the original one was bloated not without my fault.
Posts: 198
Threads: 17
Joined: Jun 2020
OK thank you. My issue now is that it is putting fields into username and password but they are both NULL. Any idea why? Thanks
Posts: 1,838
Threads: 2
Joined: Apr 2017
I don't know why you'd use different placeholders than the docs suggest and assume it works. I can't explain why no exception is thrown and don't have time to dig into the source, but I'd at least try using the placeholders mentioned in the docs to rule that possibility out (or in).
Posts: 198
Threads: 17
Joined: Jun 2020
Jun-09-2020, 05:45 AM
(This post was last modified: Jun-09-2020, 05:50 AM by card51shor.)
db.execute("INSERT INTO users (username, password) VALUES (:username, :password)", {"username":username, "password":password})
What should this be then? I can't figure it out with the documentation u sent. Before it used to give me a syntax error before i corrected it now I'm not getting an error but the values are NULL still.
Tried this:
db.execute("INSERT INTO users (username, password) VALUES (%s, %s)", ("username", "password",))
Now I get an error when I click the register button:
AttributeError: 'tuple' object has no attribute 'keys'
|