简体   繁体   English

ajax和php更新mysql

[英]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. 这是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 这是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. 这是我也试图发布数据的downloadcount.php,因此它会更新内容。

<?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());

}

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. 这就需要创建一个不可见的iframe,将其作为下载文件。 after starting this download, the ajax request is made. 开始下载后,将发出ajax请求。 a single event do the two things. 一个事件可以做两件事。 made this way the stuff still works if js is disabled. 这样,如果禁用了js,这些东西仍然可以使用。

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

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 首先,我不会亲自将jQuery和PHP混合使用,这可能是您问题的根源,我会尝试类似的方法

<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 用这样的jQuery

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

It is possible that your event is not actually being bound to the anchor, because you are running .bind() before the anchor exists. 您的事件可能没有实际绑定到锚,因为您在锚存在之前正在运行.bind() Try this: 尝试这个:

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

how many anchors you have with id = 'linkcounter' ? 您有多少个id ='linkcounter'的锚?

If you have more than one, i recommend you to change 'id' for 'class' 如果您有多个,我建议您将“类别”更改为“ id”

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM