Robert
Robert

Reputation: 925

ajax and php updating mysql

i am having problem updating the database.

Basically what i am trying to do is everytime a download link is clicked the download count in the database goes up.

Can someone point me in the right direction as i have been trying to get this right for hours :(

Here is the html.

<div class="button_border_dark"> <a id="linkcounter" href="http://www.derby-web-design-agency.co.uk/freeby-download/<?php echo $freebies_download ; ?>" target="_blank" title="Download">Click To Download File</a></div>

Here is the jquery

<script>
$('#linkcounter').bind('click',function(){
    $.post("downloadcount.php",{ linkid: <?php echo $id ; ?>});
});
</script>

Here is the downloadcount.php which i am trying to post data too, so it updates the content.

<?php
require_once("applications/constants/connection.php");
require_once("applications/controllers/basic.php");
if(isset($_REQUEST["linkid"])){

$linkid = sanitise($_POST["linkid"]);

$updatedownload = mysql_query("UPDATE freebies SET download_count=`download_count` +1 WHERE id ='".$linkid."'") OR die(mysql_error());

}

Upvotes: 1

Views: 819

Answers (5)

Jorge Olivares
Jorge Olivares

Reputation: 1492

how many anchors you have with id = 'linkcounter' ?

If you have more than one, i recommend you to change 'id' for 'class'

Greatings.

EDIT:

Try something like this:

<a href="http://www.google.cl" onclick="goToLink(this, 1);">Link</a>

<script type="text/javascript">
function goToLink(o, id) {
    $.post("downloadcounter.php",
           {linkid : id},
           function () {
               window.open($(this).attr("href"));
           }
    );

    return false;
}
</script>

Upvotes: 0

Tei
Tei

Reputation: 1416

You don't say what is the problem!

Is not incrementing? is incrementing too much? is one process blocking the other? the problem is that people can cheat and make so a file has ben downloaded a million times?

Anyway, I think you code can be simpler.

$('#linkcounter').click(function(){
    $("#invisibleiframe").attr("src",$(this).attr("src");
    $.post("downloadcount.php",{ linkid: <?php echo $id ; ?>});
    return false;
});

this need to create a invisible iframe, that will be the one downloaded the file. after starting this download, the ajax request is made. a single event do the two things. made this way the stuff still works if js is disabled.

Upvotes: 1

bfavaretto
bfavaretto

Reputation: 71918

It is possible that your event is not actually being bound to the anchor, because you are running .bind() before the anchor exists. Try this:

<script>
$(document).ready(function(){
    $('#linkcounter').bind('click',function(){
        $.post("downloadcount.php",{ linkid: <?php echo $id ; ?>});
    });
})
</script>

Upvotes: 0

r-sal
r-sal

Reputation: 1169

Well first off I would not mix the jQuery and PHP personally, this may be the source of your problem, I would try something like this

<div class="button_border_dark" theLinkId="<?php echo $id ; ?>">
    <a id="linkcounter" href="http://yourURL/" target="_blank" title="Download">
      Click To Download File
    </a>
</div>

With the jQuery like this

<script>
$('#linkcounter').bind('click',function(){
    var theLinkId = $(this).parent().attr('theLinkId');
    $.post("downloadcount.php",{ linkid : theLinkId});
});
</script>

Upvotes: 0

Pau Kiat Wee
Pau Kiat Wee

Reputation: 9505

I think should be

$updatedownload = mysql_query("UPDATE freebies SET download_count= download_count +1 WHERE id ='".$linkid."'") OR die(mysql_error());

Note the single quote:

SET download_count = download_count +1

Upvotes: 0

Related Questions