I'm having difficulties on debugging why this voting-script I'm trying to adapt wont update the vote count and register the user's IP-address in the database. As of now neither of the two things seem to be working.
The script consists of:
Index.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
$(".vote").click(function()
{
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var parent = $(this);
if(name=='up')
{
$(this).fadeIn(200).html('');
$.ajax({
type: "POST",
url: "up.php",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
} });
}
else
{
$(this).fadeIn(200).html('');
$.ajax({
type: "POST",
url: "down.php",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}
});
}
});
});
</script>
<style type="text/css">
#main{
height:auto;
margin:0 auto;
width:500px;
margin-bottom: 10px;
}
.clearfix{
clear:both;
}
#left{
float:left;
width:40px;
height:auto;
text-align:center;
}
#message{
float:right;
width:460px;
height:auto;
margin-top: 14px;
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
font-weight:bold;
}
.up{
background-image:url(up.png);
}
</style>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include('config.php');
$sql=mysql_query("SELECT * FROM messages");
while($row=mysql_fetch_array($sql))
{
$message=$row['msg'];
$mes_id=$row['mes_id'];
$totalvotes=$row['totalvotes'];
?>
<div id="main">
<div id="left">
<span class='up'><a href="" class="vote" id="<?php echo $mes_id; ?>" name="up"><img src="up.png" alt="Down" /></a></span><br />
<?php echo $totalvotes ?><br />
<span class='down'><a href="" class="vote" id="<?php echo $mes_id; ?>" name="down"><img src="down.png" alt="Down" /></a></span>
</div>
<div id="message">
<?php echo $message ?>
</div>
<div class="clearfix"></div>
</div>
<?php } ?>
</body>
</html>
And up.php and down.php that is similar except from the add/subtract part in the sql command.
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include("config.php");
$ip=$_SERVER['REMOTE_ADDR'];
$id=$_POST['id'];
$id = mysql_escape_String($id);
$ip_sql=mysql_query("SELECT ip_add FROM voting_ip WHERE mes_id_fk='$id' AND ip_add='$ip'");
$count=mysql_num_rows($ip_sql);
if($count==0)
{
$sql = "UPDATE messages SET totalvotes=sum(totalvotes+1) WHERE mes_id='$id'";
mysql_query($sql);
$sql_in = "INSERT INTO voting_ip (mes_id_fk,ip_add) VALUES ('$id','$ip')";
mysql_query($sql_in);
}
else
{
}
?>
Online link here: http://itu.dk/people/mbaj/dwdeksamen/voting/voting/
Thanks in advance.