Flask Registration Form Condition if elif else doesn't work
<p>When I try to register an user, I don't know why, but if the email I'm trying to register does not exist in the db, the for loop ends, though there's an elif end else statement.</p>

<p>Someone know why?</p>

<p>This is the web.py file</p>

<pre><code>from flask import Flask, session, redirect, url_for, escape, request, render_template
from wtforms import Form, BooleanField, TextField, PasswordField, validators
import MySQLdb
import logging


app = Flask(__name__)


db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="usbw", port=3307, db="tourme")
cur = db.cursor()


@app.route('/register/', methods=["GET","POST"])
def register():

    email_msg = None
    success_msg = None
    pass_msg = None
    test = None

    try:

        if request.method == 'POST':

            id_id = None
            username = request.form['name']
            lastname = request.form['lastname']
            email = request.form['email']
            password = request.form['password']
            repeat = request.form['repeat']

            cur = db.cursor()
            cur.execute("SELECT * FROM users WHERE email = (%s)", [email])

            for row in cur.fetchall():
                if row[1] == email:
                    email_msg = "This e-mail already exist: " + row[1]

                elif password == repeat:
                    cur.execute("INSERT INTO users VALUES (%s,%s,%s,%s,%s)", (id_id,email,username,lastname,password))
                    db.commit()
                    success_msg = "Bravo"
                    db.close()

                else:
                    pass_msg = "Password must match"

        return render_template("register.html", email_msg=email_msg, success_msg=success_msg, pass_msg=pass_msg)

    except Exception as e:
        return(str(e)) 


app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'

if __name__ == '__main__':
    app.run(debug=True)
</code></pre>

<p>And this is the template</p>

<pre><code>&lt;div class="register"&gt;    
    &lt;form action="" method="POST"&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-addon" id="basic-addon3"&gt;Your Username&lt;/span&gt;
            &lt;input type="text" class="form-control" name="name" aria-describedby="basic-addon3"&gt;
        &lt;/div&gt;
        &lt;br&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-addon" id="basic-addon3"&gt;Your Lastname&lt;/span&gt;
            &lt;input type="text" class="form-control" name="lastname" aria-describedby="basic-addon3"&gt;
        &lt;/div&gt;
        &lt;br&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-addon" id="basic-addon3"&gt;Email&lt;/span&gt;
            &lt;input type="email" class="form-control" name="email" aria-describedby="basic-addon3"&gt;
        &lt;/div&gt;
        &lt;br&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-addon" id="basic-addon3"&gt;Your Password&lt;/span&gt;
            &lt;input type="password" class="form-control" name="password" aria-describedby="basic-addon3"&gt;
        &lt;/div&gt;
        &lt;br&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-addon" id="basic-addon3"&gt;Repeat Password&lt;/span&gt;
            &lt;input type="password" class="form-control" name="repeat" aria-describedby="basic-addon3"&gt;
        &lt;/div&gt;
        &lt;br&gt;
        &lt;input type="Submit" value="Register" class="btn btn-default btn-sm"&gt;
    &lt;/form&gt;
    {% if success_msg %}
        &lt;p class=success_msg&gt;&lt;strong&gt;Message:&lt;/strong&gt; {{ success_msg }}
    {% endif %}
    {% if email_msg %}
        &lt;p class=success_msg&gt;&lt;strong&gt;Error:&lt;/strong&gt; {{ email_msg }}
    {% endif %}
    {% if pass_msg %}
        &lt;p class=pass_msg&gt;&lt;strong&gt;Error:&lt;/strong&gt; {{ pass_msg }}
    {% endif %}    
    &lt;br&gt;
    &lt;a href="{{ url_for('index') }}"&gt;Home&lt;/a&gt;
&lt;/div&gt;
</code></pre>

<p>Thanks a lot for your help!</p>

<p>Like I said in the comment, this is hardly a programming problem, it's a logic problem.</p>

<pre><code>cur.execute("SELECT * FROM users WHERE email = (%s)", [email])
</code></pre>

<p>That will give you all users that already use this email. So there are 2 possibilities : </p>

<ol>
<li>There is at least one user (hopefully you put a unique constraint on email so there is exactly one) that uses this email. Since the queryset is not empty, you enter the loop, obviously you also enter the if, since you compare the row to the email you used on the request to get the row. Finally you get the expected behavior.</li>
<li>No user have this email. Well then, the problem is the queryset comes up empty, so you don't enter the loop and subsequently you never reach your <code>INSERT INTO</code>. That's the logic problem : your insert should happen only if the queryset is empty but if it is, your code can't reach the insert.</li>
</ol>

<p>I don't know how you tried without loop but this should works :</p>

<pre><code>cur.execute("SELECT * FROM users WHERE email = (%s)", [email])
if cur.fetchone():
    email_msg = '...'
elif password == repeat:
    cur.execute('...')
else:
    pass_msg = '...'
</code></pre>

python if-statement flask