Search and insertion do not work together - Flask

1
Hello, I'm doing a job that needs to insert values into the database, return them in a table and be able to delete them through Flask. I'm getting to know the tool and I can not find solutions. Getting the insertion on one page and the visualization and deletion on another, all working properly, but the problem comes when together in a single page, where the insertion only works.

This is part of HTML:

<table class="table">
        <thead>
            <tr>
                <th scope="col">ID</th>
                <th scope="col">LINK / IP BLOQUEADO</th>
                <th scope="col">REMOVER</th>
            </tr>
        </thead>
        <tbody>
            {% for row2 in listaIpLinks %}
            <tr>
                <th scope="row">{{row2.0}}</th>
                <td>{{row2.1}}</td>
                <td>
                    <form method="post" action="{{ url_for('deleteIpLink', id=row2.0) }}">
                        <input type="submit" value="Delete"></i>
                    </form>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    {%from "_formhelpers.html" import render_field %}
    <form method="post" action="{{ url_for('ipLink') }}">
        <dl>
            {{render_field(form.ipLink)}}
        </dl>
        <p><input type="submit" value=Register></p>
    </form>

Here's the part responsible for Flask:

@app.route('/getIpLink/', methods=["GET","POST"])
def ipLink():
form = ipLinkForm(request.form)
try:
    if form.validate():
        ipLink = form.ipLink.data
        mycursor,mydb = connection()

        mycursor.execute('INSERT INTO linksespecificos(ALVO) VALUES ("'+ipLink+'")')
        mydb.commit()
        flash("LINK/IP inserido com sucesso")
        mycursor.close()
        mydb.close()
        gc.collect()
        return redirect(url_for("getIpLink"))
    return render_template("getIpLink.html", form=form)

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

@app.route('/getIpLink/')       
def getIpLink():
try:
    mycursor,mydb = connection()
    mycursor.execute('SELECT * FROM linksespecificos')
    data = mycursor.fetchall()
    mycursor.close()
    return render_template("getIpLink.html", listaIpLinks=data)
except Exception as e:
    return(str(e))  


@app.route('/getIpLink/<id>', methods=["GET","POST"])   
def deleteIpLink(id):
try:
    mycursor,mydb = connection()
    mycursor.execute('DELETE FROM linksespecificos WHERE ID="'+id+'"')
    mycursor.close()
    flash("Link deletado!")
    return redirect(url_for('getIpLink'))
except Exception as e:
    return(str(e))

I apologize, but I'm starting now in the area and I'm not very knowledgeable, I'm proud to have arrived here, but I got stuck in that part. If anyone can help, I would be greatly grateful!

    
asked by anonymous 31.10.2018 / 22:55

1 answer

1

I recommend that you do so:

consulta = "INSERT INTO linksespecificos(ALVO) VALUES (%s)"
cursor.execute(consulta,(ipLink,))

consulta = "DELETE FROM linksespecificos WHERE ID= %s"
cursor.execute(consulta,(id,))

Even if it is a parameter passed, place a comma at the end.

    
01.11.2018 / 13:06