我在调试为什么我正在尝试调整的投票脚本不会更新投票计数并在数据库中注册用户的IP地址时遇到困难。截至目前,这两件事似乎都没有起作用。
该脚本包括:
的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>
up.php 和 down.php 除了sql命令中的add / subtract部分之外是类似的。
<?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
{
}
?>
在线链接:http://itu.dk/people/mbaj/dwdeksamen/voting/voting/
提前致谢。