UPDATE MySQL with 'onclick' button command in Django Project
<p>I am extremely new to django and web dev on the whole, so please bear with me.</p>

<p>I have created a simple site with a MySQL backend for my local football team and want to create a page to update the score of a game (stored on a table)  simply by clicking a button (increment the current score by + 1).
I have no doubt in my mind that this is super simple, but after WEEKS of trawling through similar posts, nothing seemed to work for me (nothing that I could understand at least).</p>

<p>I have a template that that creates a button that contains the ID for the record that needs to be updated:</p>

<pre><code>&lt;a href="{% url livegame_update %}?value={{stat.id}}?updatetype=goals" class="btn btn-success" role="button" onclick="alert({{stat.id}})" id={{stat.id}}&gt;Goal&lt;/a&gt;
</code></pre>

<p>This points to this URL:</p>

<pre><code>url(r'^livegame_update', 'steelers_fc.players.views.livegame_update', name='livegame_update'),
</code></pre>

<p>Which in turn executes this function the Views;</p>

<pre><code>def livegame_update(request):
StatID = request.GET.get('value','1')
StatType = request.GET.get('updatetype','1')
SQL = "update players_statistics set " + StatType + " = " + StatType + " + 1 where id = " + StatID + ";"
#stat_edit = statistics.objects.get(id=StatID)
#stat_edit.goals = stat_edit.goals + 1
#stat_edit.save() # save object

cursor = connection.cursor ()
cursor.execute (SQL)
connection.commit()
cursor.close ()
connection.close ()

return render_to_response
</code></pre>

<p>Ultimately, the SQL above would end up being:
 "<em>update players_statistics set goals = goals + 1 where id = 99</em>"</p>

<p>I have tried several things:</p>

<ul>
<li>Execute RAW SQL (as per this example)</li>
<li>Update Django Model (as per the commented out section)</li>
<li>Executed an external .py script (which works perfectly well from a Bash Comamnd)</li>
<li>Looked at using 'forms'..but never really understood.</li>
</ul>

<p>Ultimately, I feel the issue is around the URL and the passing of parameters between the request and the view?!?!</p>

<p>Any help would be greatly appreciated and some simple examples would be amazing.</p>

<p>Thanks</p>

<p>Well... First of all I do not understand why are you creating cursors and stuff. </p>

<p>I`ll try to help you out here in most easy way I can imagine.</p>

<p><strong>STEP 1 (if you do not have model)</strong></p>

<p>Create model</p>

<pre><code>class Match(models.Model):
    goals = models.IntegerField(default=)
    # Add as many fields as you need, eg. Teams / Scorer / and other stuff

    def __str___(self):
    return self.goals
</code></pre>

<p><strong>STEP 2 - urls.py / you have, but some parts are missing</strong></p>

<pre><code>url(r'^livegame_update/(?P&lt;match_id&gt;[\d]+)/$', 'steelers_fc.players.views.livegame_update', name='livegame_update'),
</code></pre>

<p>Explanation: As you can see your url now takes match_id as parameter passed to your view. The part with <strong>[\d]+</strong> - says it has to be a number, plus means unlimited number.</p>

<p><strong>STEP 3 - Your view (update)</strong></p>

<pre><code>def livegame_update(request, match_id):
    try:
        match = Match.objects.get(id=match_id)
    except Match.DoesNotExist():
        return render(request, 'your_template.html', {'error': 'match with given id dosnt exist})
    match.goals += 1
    match.save()

    return render(request, 'your_template.html', {'msg': 'incremented score'})
</code></pre>

<p>so simply what this does is: Find me a Match object with id given in parameter (<strong>match_id</strong>). If it is not found, return to your template, and pass a dict with an error. If it exists increment your <strong>goals</strong> field by one. Then save your object. Finally return to your template with buttons and stuff.</p>

<p>Is it clear ? Do you need to explain anything in above code ? </p>

javascript python html mysql django