0

So I am a newbie but working on a registration system form in flask/MYSQL

I am receiving this error (UnboundLocalError: local variable 'cursor' referenced before assignment) only when I tried to update it from webpage.

when I use postman its fine.

After hours of playing with the code and research I need your help.

This is my file, please let me know if theres anything else I need to share. thank you

@app.route('/updateHdd', methods=['POST'])
def updateHdd():
try:
    if session.get('user'):
        _user = session.get('user')
        _keterangan = request.form['editKeterangan']
        _nomor = request.form['nomor']



        conn = mysql.connect()
        cursor = conn.cursor()
        cursor.callproc('sp_updateHdd',(_user,_keterangan,_nomor))
        data = cursor.fetchall()

        if len(data) is 0:
            conn.commit()
            return json.dumps({'status':'OK'})
        else:
            return json.dumps({'status':'ERROR'})
except Exception as e:
    return json.dumps({'status':'Unauthorized access'})
finally:
    cursor.close()
    conn.close()    

here is my script in html:

$(function(){

            GetItems();
            $('#btnUpdate').click(function(){
                $.ajax({
                    url : '/updateHdd',
                    data : { 
                        keterangan: $('#editKeterangan').val(),
                        nomor: localStorage.getItem('editNomor')
                    },
                type : 'POST',
                success: function(res){
                    $('#edit').modal('hide');
                },
                error: function(error){
                    console.log(error);
                }
                });
            });
        });

another script incase you you guys need:

function Edit(elm){
        localStorage.setItem('editId',$(elm).attr('data-id'));
            $.ajax({
                url : '/getWishBynomor',
                data : {id:$(elm).attr('data-id')},
                type : 'POST',
                success: function(res){
                    var data = JSON.parse(res);
                    $('#editKeterangan').val(data[0]['Keterangan']);
                    $('#editModal').modal();
                },
                error: function(error){
                    console.log(error);
                }
            });
        }

and this is the html code:

<div class="modal fade" id="editmodal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="editModalLabel">Update Data</h4>
                </div>
                <div class="modal-body">
                    <form role="form">
                        <div class="form-group">
                            <label for="message-text" class="control-label">Keterangan:</label>
                            <textarea class="form-control" id="editKeterangan"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button id="btnUpdate" type="button" class="btn btn-primary">Update</button>
                </div>
            </div>
        </div>
    </div>

I really need your help guys

snow
  • 15
  • 4
  • 1
    Think what would happen in your finally-block, if `session.get('user')` is falsy (and the message of the exception is the answer). – Ilja Everilä Nov 13 '17 at 07:24
  • but when i use postman its work just fine, in mysql receive the update @IljaEverilä – snow Nov 13 '17 at 07:31
  • 1
    Perhaps you send a valid session id / token in your postman test. Still, think about it: what will happen, if `session.get('user')` is falsy, i.e. None or such. Where is `cursor` first assigned? – Ilja Everilä Nov 13 '17 at 07:34
  • so what sould i remove? i tried to remove 'session.get('user')' and '#finally: #cursor.close() #conn.close()' – snow Nov 13 '17 at 07:42
  • im getting confuse – snow Nov 13 '17 at 07:45
  • could you show me on my code? please... – snow Nov 13 '17 at 07:50
  • The point is that while the if-block might not be entered, or something in it might raise an exception before the assignment to `cursor`, like `request.form['editKeterangan']` because in your Javascript you send just `keterangan: $('#editKeterangan').val(),`, the finally-block is always run. Hence when you enter the finally-block `cursor` might never have been assigned to and so the name has not been bound to. Read the answers from the dupe targets, especially the latter. – Ilja Everilä Nov 13 '17 at 07:55
  • aaah, thanks. i get it. thank you so much – snow Nov 13 '17 at 08:00

0 Answers0