PHP&;MySQL分级脚本页面问题?
我在about.com上找到了这个脚本,我正试图从中学习如何创建一个评级系统,但由于某种原因,当点击链接时,这个脚本不会计算投票数,它会将我发送到一个显示“未找到”的页面。页面只需重新加载到自身并计算投票> 我想知道如何解决这个问题?我需要更改代码的哪一部分?在哪里 下面是我也被发送的页面的样子PHP&;MySQL分级脚本页面问题?,php,mysql,Php,Mysql,我在about.com上找到了这个脚本,我正试图从中学习如何创建一个评级系统,但由于某种原因,当点击链接时,这个脚本不会计算投票数,它会将我发送到一个显示“未找到”的页面。页面只需重新加载到自身并计算投票> 我想知道如何解决这个问题?我需要更改代码的哪一部分?在哪里 下面是我也被发送的页面的样子 Not Found The requested URL /New was not found on this server. 下面是脚本 <?php // Connects to your
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>";
}
?>
$\u服务器['PHP\u SELF']
的价值是什么?它包含空格吗?您需要在href值周围加引号,并对所有HTML实体(如&)进行编码,因此最后一行如下所示:
echo "<a href=\"".htmlentities($_SERVER['PHP_SELF']."?mode=vote&voted=5&...").\"">Vote</a>";
echo”“;
来自php文档:
当前执行脚本的文件名,相对于文档根。例如,地址处脚本中的$\u SERVER['PHP\u SELF']将
只需删除该内容并自己编写脚本的路径。您应该在那里使用urlencode
函数,而不是htmlentities
函数。您不应该包括查询字符串,因为?
和&
将被编码为,从而使它应该发送的GET数据无效。不包括查询字符串是什么意思?对不起,我不知道我是新来的。Atli是说你的最后一行应该是这样的:echo”“
上面示例中的urlencode()
将对?和&characters转换为ASCII(我想是吧?)等价物,这使得它们对PHP毫无用处。我试过了,现在它将我的正则URL从http://localhost/new 文件/8.php
到http://localhost/new file/%2Fnew+fileProject%2F8.php?mode=vote&voted=2&id=3
@Atli:我想你混淆了两件事。(事实上,他需要同时做这两件事,我们每个人都在描述不同的事情)。第一步是urlencode
获取参数,如果$ratings['id']
包含f.i.=or?,则需要获取参数?。第二步是对HTML中具有特殊含义的字符进行编码(在正确编码的URL中,我认为唯一可以留下的是&)。评级必须是一个流行话题。在过去的两天里,我注意到了5个关于使用php和mysql构建评级系统的问题。