Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在jquery中获取数据库检索值?_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Php 如何在jquery中获取数据库检索值?

Php 如何在jquery中获取数据库检索值?,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我想在用户点击likesame as facebook like时更新likes on数据库。我将从数据库中加载各种帖子。对于每个帖子,数据库中都有一个唯一的字段,名为midmessage Id,当用户单击帖子下方的like时,我想通过mid在数据库中增加该特定帖子的like。我想使用jquery实现此功能,因为如果我通过url传递mid,它将导航到该页面并加载整个页面,所以我需要通过AJAX调用在页面后面完成。让我展示一下我的数据库检索是如何进行的 $cot = "select * from

我想在用户点击likesame as facebook like时更新likes on数据库。我将从数据库中加载各种帖子。对于每个帖子,数据库中都有一个唯一的字段,名为midmessage Id,当用户单击帖子下方的like时,我想通过mid在数据库中增加该特定帖子的like。我想使用jquery实现此功能,因为如果我通过url传递mid,它将导航到该页面并加载整个页面,所以我需要通过AJAX调用在页面后面完成。让我展示一下我的数据库检索是如何进行的

$cot = "select * from posts where userid = $usr LIMIT 10";
$ex = mysql_query($cot, $con);
while($cont = mysql_fetch_array($ex))
{
    $date = date_create($cont['date']);
    $mid = $cont['mid'];

    echo "<div id='posts'>";
    echo $cont['message'];
    echo $photo;
    echo "<div class='like'>"; //echo $mid; /* It is to show message id*/ 
    echo "<a href='#'>Like</a></div>";  //When Clicked Like i Want to increment likes on DB
    echo "Likes(" . $cont['Likes'] . ")";
    echo date_format($date, 'd-m-Y H:i:s');
    echo "<hr>";
    echo "</div>";
}
我希望通过jquery和ajax调用完成这项工作。我只需要jquery代码来调用php文件increment.php并将中间消息Id传递给该页面

$.post('/increment.php', { mid: whatever });
使用您的示例单击处理程序,很容易将其添加到

 $(document).ready(function() { 
     $("div.pray").click(function() { 
          var mid=$(this).val(); 
          alert(mid);
          $.post('/increment.php', { mid: mid });
     });
 });

也许你需要这样的东西:

    echo "<a href='javascript:void(0);' class='like' data-mid='".$cont['mid']."'>Like</a></div>";  //When Clicked Like i Want to increment likes on DB
下面是脚本:

    <script type="text/javascript">
    $(function(){
        $(".like").live('click', function(){
           $.ajax({
                url     : 'increment.php',
                data    : {'mid':$(this).data('mid')},
                type    : 'POST',
                success : function(resp){
                    if(resp == '1'){
                        //success message or whatever
                    }
                },
                error   : function(resp){
                    alert("some error occured !");
                }
           }); 
        });
    });
    </script>
在increment.php上:

    <?php
        $id = $_POST['mid'];
        $sql = "update posts set Likes = Likes+1 where mid = '".$id."'";
        //execute the above and return some thing
        return 1;
    ?>

请查看文档以了解更多信息。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,谢谢。我正在读它的特点。我的疑问是,若服务器并没有更新到PHP5,那个么我必须使用mysql?我需要如何在jquery中获得mid$document.readyfunction{$div.pray.clickfunction{var mid=$this.val;alertmid;};像这样,对应的消息id。