我在about.com上发现了这个脚本,我正在尝试学习如何创建一个评级系统但是由于某种原因,脚本在点击链接时不会计算投票,并会将我发送到一个页面上未找到。页面应该重新加载到自己并计算投票>
我想知道如何解决这个问题?我需要改变哪些代码以及在哪里?
以下是我发送的页面的样子。
Not Found
The requested URL /New was not found on this server.
以下是以下脚本。
<?php
// Connects to your Database
mysql_connect("localhost", "root", "", "sitename") or die(mysql_error());
mysql_select_db("sitename") or die(mysql_error());
//We only run this code if the user has just clicked a voting link
if ( $mode=="vote")
{
//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
echo "Sorry You have already ranked that site <p>";
}
//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month);
//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE vote SET total = total+$voted, votes = votes+1 WHERE id = $id");
echo "Your vote has been cast <p>";
}
}
//Puts SQL Data into an array
$data = mysql_query("SELECT * FROM vote") or die(mysql_error());
//Now we loop through all the data
while($ratings = mysql_fetch_array( $data ))
{
//This outputs the sites name
echo "Name: " .$ratings['name']."<br>";
//This calculates the sites ranking and then outputs it - rounded to 1 decimal
if($ratings['total'] > 0 && $ratings['votes'] > 0) {
$current = $ratings['total'] / $ratings['votes'];
}
else{
$current = 0;
}
echo "Current Rating: " . round($current, 1) . "<br>";
//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item
echo "Rank Me: ";
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings['id'].">Vote 1</a> | ";
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings['id'].">Vote 2</a> | ";
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings['id'].">Vote 3</a> | ";
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings['id'].">Vote 4</a> | ";
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings['id'].">Vote 5</a><p>";
}
?>
答案 0 :(得分:0)
$_SERVER['PHP_SELF']
的价值是多少?它是否包含空格?您需要在href值周围加上引号,并对所有HTML实体(例如&amp;)进行编码,因此最后一行变为如下所示:
echo "<a href=\"".htmlentities($_SERVER['PHP_SELF']."?mode=vote&voted=5&...").\"">Vote</a>";
答案 1 :(得分:0)
来自php文档:
当前正在执行的脚本的文件名,相对于文档根目录。例如,地址http://example.com/test.php/foo.bar的脚本中的$ _SERVER ['PHP_SELF']将
只需擦除那个东西并自己编写脚本的路径。