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">×</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