Inserting multiple id's in a mysql query?

441 views Asked by At

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

There are 1 answers

0
MamaWalter On BEST ANSWER

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)