douxiong4250 2014-01-08 14:08
浏览 39
已采纳

在mysql查询中插入多个id?

I'm trying to do a like/unlike function for a website. I made a single working one which is connected to a database table called 'posts'.

The table 'posts' has 4columns called id, name, like, unlike.

My problem is that I now want to use the same code but to use it in another table called 'venue'. This table has the same 4 columns, but they are called vID, Location, like, unlike. And the table 'venue' has several vIDs, instead of a single id like the table 'posts'.

Can't figure out what to do?:


    <html>
<head>
    <script src = "http://code.jquery.com/jquery-1.3.2.min.js"></script>
    <script>
        function doAction(postid,type){
            $.post('doAjax.php',{postid:postid, type:type}, function(data){
                //if(isNaN(parseFloat(data))){
                    //alert(data);
                //}else{
                    $('#'+postid+'_'+type+'s').text(data);
                });
            }
        //}
    </script>
</head>
<body>
    <?php

        include('db.php'); // including database connection
        $postid= 1;
        $data = mysql_fetch_object(mysql_query('SELECT `like`, `unlike` from     posts WHERE id ="'.$postid.'"'));  

    ?>
    <a href="javascript:;" onclick="doAction('<?php echo $postid;?>','like');">Like (<span id="<?php echo $postid;?>_likes"><?php echo $data->like;?></span>)</a>
    <a href="javascript:;" onclick="doAction('<?php echo $postid;?>','unlike');">Unlike (<span id="<?php echo $postid;?>_unlikes"><?php echo $data->unlike;?></span>)</a>


    </body>


   <?php

include('db.php');

if($_POST['postid'] !='' && $_POST['type'] != ''){

        if($_POST['type']=='like'){
            mysql_query(' UPDATE posts SET `like`=`like`+1 WHERE id="'.(int)$_POST['postid'].'"');
            $num = mysql_fetch_row(mysql_query(' SELECT `like` FROM  posts WHERE id="'.(int)$_POST['postid'].'" LIMIT 1'));
        }elseif($_POST['type']=='unlike'){
            mysql_query(' UPDATE posts SET `unlike`=`unlike`+1 WHERE id="'.(int)$_POST['postid'].'"');
            $num = mysql_fetch_row(mysql_query(' SELECT `unlike` FROM  posts WHERE id="'.(int)$_POST['postid'].'" LIMIT 1'));
        }
                     echo $num[0];
                    (int)$_POST['postid'].'","'.$_SERVER['REMOTE_ADDR'].'")');
    }
      ?>    
  • 写回答

1条回答 默认 最新

  • dqknycyt92288 2014-01-08 15:23
    关注

    I'm not sure to understand your problem. If it's generate buttons from the data in the table "venue", you can try something like this:

    <?php
    
    $query = "SELECT * from venue";
    
    if ($result = $mysqli->query($query)) {
    
        while ($obj = $result->fetch_object()) {
    ?>      
        <a href="javascript:;" onclick="doAction('<?php echo $obj->id;?>','like');">Like (<span id="<?php echo $obj->id;?>_likes"><?php echo $obj->like;?></span>)</a>
        <a href="javascript:;" onclick="doAction('<?php echo $obj->id;?>','unlike');">Unlike (<span id="<?php echo $obj->id;?>_unlikes"><?php echo $obj->unlike;?></span>)</a>
    <?php
        }
    
        $result->close();
    }
    ?>
    

    and mysql_fetch_object is deprecated, you should use mysqli_fetch_object (http://php.net/manual/en/function.mysql-fetch-object.php)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题