如何在不刷新页面的情况下使用PHP和AJAX更新MySQL [英] How to Update MySQL with PHP and AJAX without REFRESHING the PAGE

查看:64
本文介绍了如何在不刷新页面的情况下使用PHP和AJAX更新MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,这是我的问题

我有按以下顺序排列的MySQL:

I have MySQL with the following order:

ids-广播-链接-时间-艺术家-标题-不喜欢

ids - radio - link - time - artist - title - disliked

ids是媒体的ID

ids is the ID of the Media

从LISTEN.php页开始,我可以从数据库中随机选择视频.

from page LISTEN.php I have random selection of video from the Database.

**我需要在LISTEN.php上有一个按钮,当有人单击它时,例如按钮的名称->不喜欢

**I need to have a button on LISTEN.php where when somebody clicks it for example the name of the button - > Dislike

因此,如果有人单击它的AJAX或SOMEHOW来不刷新页面,但同时在MySQL中单击DISLIKE按钮以更新(再次刷新该页面时又一次),则刷新为不喜欢-1(当此按钮为再按一次,同一视频将更新为2/3/4,依此类推.现在我所有的视频均为0.

so if someone clicks it AJAX or SOMEHOW not to refresh the page but in the same time when the DISLIKE button is clicked to Update in the MySQL (once again without refreshing the page) to Disliked - 1, (when this button is pressed again for the same video to update to 2 / 3 / 4 and so on. Right now all of my videos are 0.

例如,我需要能够查看不喜欢的视频

I need to be able to View not LIKED videos, for example

从RANDOM中选择*,其中DISLIKED是更高的数字,然后是> 0 **

select * from RANDOM where DISLIKED is Higher number then >0**

我在PHP方面不是很好,所以请帮助我,再过一次,页面应该不刷新.

I am not a very good in PHP so please help me, one more time the PAGE SHOULD NOT REFRESH.

非常感谢您的帮助.

推荐答案

jQuery中的Ajax就像这样:

Ajax in jQuery works like this:

var myData=1;
$.ajax({
    type:'POST',//type of ajax
    url:'mypage.php',//where the request is going
    data:myData,//the variable you want to send
    beforeSend:function(xhr){//as a standard, I add this to validate stuff
        if(someThingWrong===true)xhr.abort//aborts xhttpRequest
   },
   success:function(result){
       //result is your result from the xhttpRequest.
   }
});

这不会刷新您的页面,但会向指定的网址发送"POST".在您指定的页面上,您想做任何您想做的事情并说返回结果.在我的示例中,我将做一些简单的事情:

This will not refresh your page but send a 'POST' to the url specified. On your specified page you want to do whatever it is you want to do and say return a result. In my example I'll do something simple:

if($_POST['myData']===1)return True;

这是使用jQuery进行AJAX请求的基础.

That's the basics of an AJAX request using jQuery.

编辑!

启动AJAX脚本: 我只是在猜测,因为我不知道您的html中的元素还是您的脚本中的任何元素!因此,您必须进行调整!

initiating an AJAX script: I'm only guess as I don't know your elements within your html nor your scripts what so ever! So you'll have to make adjustments!

$('button.dislike').click(function(){
    $.ajax({
        type:'POST',
        url:'disliked.php',
        data:{dislike:$(this).attr('id')},
        success:function(result){
            $(this).prev('span').append(result);
        }
    });
 });

PHP: 不要使用mysql,它已被贬值,被认为是不正确的做法,我也不知道为什么在查询中使用sprintf? :S

PHP: don't use mysql, it's now been depreciated and is considered bad practise, I also don't know why're using sprintf on the query? :S

$DBH=new mysqli('location','username','password','database');
$get=$DBH->prepare("SELECT dislike FROM random WHERE ids=?");
$get->bind_param('i',$_POST['dislike']);
$get->execute();
$get->bind_result($count);
$get->close();
$update=$DBH->prepare('UPDATE random SET dislike=? WHERE ids=?');
$update->bind_param('ii',++$count,$_POST['dislike']);//if you get an error here, reverse the operator to $count++.
$update->execute();
$update->close();
return String $count++;

仅当HTML中存在一系列ID与数据库中的ID匹配的按钮时,此方法才有效.所以

This will only work if there in your HTML there is a series of buttons with ID's matching those in your database. So

$get=$DBH->prepare('SELECT ids FROM random');
$get->execute();
$get->bind_result($ids);
while($get->fetch()){
    echo"<button class='dislike' id='".$ids."'>Dislike this?</button>";
}

希望您对我如何管理不喜欢的按钮系统XD大声笑有个大概的认识

Hope you get the general idea of how I'm managing your dislike button system XD lol

这篇关于如何在不刷新页面的情况下使用PHP和AJAX更新MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆