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'].'")');
}
?>
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:
and
mysql_fetch_object
is deprecated, you should usemysqli_fetch_object
(http://php.net/manual/en/function.mysql-fetch-object.php)