Wednesday, 15 July 2015

Why is my mysql database not updating (using flask/python) -



Why is my mysql database not updating (using flask/python) -

i'm trying add together user database when come in in username , password. however, database not updating @ , of info in database stays same. i've tested out code , run mysql.connect().commit() not committing code database.

my flask file:

from flask import flask, jsonify, render_template, request, response, json, redirect, url_for flaskext.mysql import mysql import re myfunction import * get_tv_name import * mysql = mysql() app = flask(__name__, static_folder="", static_url_path='') app.config['secret_key'] = 'f34tf$($e34d'; app.config['mysql_database_user'] = 'root' app.config['mysql_database_password'] = "" app.config['mysql_database_db'] = 'accounts' app.config['mysql_database_host'] = 'localhost' mysql.init_app(app) @app.route('/') def index(): homecoming render_template('index.html') @app.route('/register.html/', methods=['get', 'post']) def register(): error = none if request.method == 'post': cursor = mysql.connect().cursor() cursor.execute("select * _accounts username='" + request.form['username'] + "' , password='" + request.form['password'] + "'") info = cursor.fetchone() if info == none: cursor.execute("insert _accounts values (null, '" + request.form['username'] + "', '" + request.form['password'] + "')") mysql.connect().commit redirect(url_for('index')) else: error = "it complete" homecoming render_template('/register.html/', error=error) if __name__ == '__main__': app.run(port=8080, debug=true)

my html:

<div class="container"> <div class="row"> <div class="col-md-6 centered"> <div class="panel panel-default"> <div class="panel-heading"> <strong class="">create account!</strong> </div> <div class="panel-body"> <form class="form-horizontal" role="form" action="" method="post"> <div class="form-group"> <label for="inputfirstname3" class="col-sm-3 control-label">first name</label> <div class="col-sm-9"> <input type="email" class="form-control" id="inputemail3" placeholder="first name" required=""> </div> </div> <div class="form-group"> <label for="inputlastname3" class="col-sm-3 control-label">last name</label> <div class="col-sm-9"> <input type="email" class="form-control" id="inputemail3" placeholder="last name" required=""> </div> </div> <div class="form-group"> <label for="inputemail3" class="col-sm-3 control-label">username</label> <div class="col-sm-9"> <input type="text" required="" placeholder="username" class="form-control" name="username" value="{{ request.form.username }}"> </div> </div> <div class="form-group"> <label for="inputpassword3" class="col-sm-3 control-label">password</label> <div class="col-sm-9"> <input type="password" class="form-control" required="" placeholder="password" name="password" value="{{ request.form.password }}"> </div> </div> <div class="form-group last"> <div class="col-md-offset-3 col-md-3"> <button type="submit" class="btn btn-success btn-sm">create account</button> </div> </div> </form> </div> </div> </div> </div>

the reason database isn't updating aren't committing transaction. create connection mysql.conn() , cursor adding .cursor(). when go commit changes, brand new connection 1 time again calling mysql.conn().

what want capture reference connection can used later. you'll want utilize parameterized query avoid things sql injection attacks.

conn = mysql.connect() cursor = conn.cursor() info = cursor.fetchone() if info none: cursor.execute("insert _accounts values (null, %s, %s)", (request.form['username'], request.form['password'])) conn.commit() redirect(url_for('index')) # i'm guessing want set homecoming here. else: error = "it complete" # snip

lastly, when comparing none you'll want utilize is operator rather equality. from pep8

comparisons singletons none should done is or is not, never equality operators.

python mysql flask

No comments:

Post a Comment