tags:

views:

48

answers:

2

I found this script on about.com which I'm trying to learn from on how to create a rating system but the script for some reason wont count a vote when the link is clicked and will send me to a page that says not found. The page should just reload to itself and count the vote>

I was wondering how can I fix this problem? And what part of the code do I need to change and where?

Below is how the page looks like that I'm sent too.

Not Found

The requested URL /New was not found on this server.

Here is the script below.

<?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>";
}
?>
A: 

What's the value of $_SERVER['PHP_SELF'] ? Does it contain a space? You need to put quotes around the href value, and encode all HTML entities (such as &), so the last lines become like this:

echo "<a href=\"".htmlentities($_SERVER['PHP_SELF']."?mode=vote&voted=5&...").\"">Vote</a>";
Wim
Atli
What do you mean not include the query string? sorry for being ignorant I'm new to this.
PeAk
Atli is saying that your last line should be more like this: `echo "<a href=\"".urlencode($_SERVER['PHP_SELF'])."?mode=vote
Jeff Rupert
PeAk
Wim
A: 

from the php documentation:

The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would

just erase that thing and write the path to the script by yourself.

Victor