PHP &

2 Cevap php

Ben bağlantı tıklandığında alışkanlık bir oy sayım nedense bir derecelendirme sistemi ancak komut dosyası oluşturmak için nasıl öğrenmek için çalışıyorum about.com bu komut dosyası bulundu ve sadece sayfa yeniden yükler.

Ben bu sorunu nasıl düzeltebilirim merak ediyorum? Ve kodun hangi parçası nerede değiştirmek gerekir ve var?

Aşağıda tam yazısıdır.

<?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>";
  } else {
        //Otherwise, we set a cooking telling us they have now voted
    $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=?mode=vote&voted=1&id=".$ratings['id'].">Vote 1</a> | ";
  echo "<a href=?mode=vote&voted=2&id=".$ratings['id'].">Vote 2</a> | ";
  echo "<a href=?mode=vote&voted=3&id=".$ratings['id'].">Vote 3</a> | ";
  echo "<a href=?mode=vote&voted=4&id=".$ratings['id'].">Vote 4</a> | ";
  echo "<a href=?mode=vote&voted=5&id=".$ratings['id'].">Vote 5</a><p>";
}
?>

2 Cevap

$mode atmadı? register globals oldu eğer çalışmış olsa da, bir daha varsayılan olarak etkin değildir (ve PHP'nin sonraki sürümlerinde kaldırılır)

//We only run this code if the user has just clicked a voting link
if ( $mode=="vote") {

Belki demek

if ( $_GET['mode']=="vote") {

Aynı $id için gider ve $voted, hangi de koymak asla.

EDIT
I also would like to add, that if I went and changed id to 1';DROP TABLE vote; You would have a whole lot of data lost. Look at SQL Injection

EDIT
If the row in the table doesn't exist, you will need to INSERT it before you can UPDATE it.

Ben de o olmalıdır koduna bakarak, $ cookie atmadı görebilirsiniz 'Mysite' . $id. PHP dizge olarak herhangi bir borsada işlem görmeyen bir metin tedavi ancak, daha sonra onun her zaman iyi bir fikir yanlış anlama ve hatalarını önlemek olacaktır ama ben dize için tırnak ekledi.

Ayrıca bu komut PHP seçenek register_globals varsayar, sen register_globals php.ini ON = emin olmak gerekir