Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Sonda - problem z dodawaniem głosó do bazy
Forum PHP.pl > Forum > Przedszkole
mefistofeles
Mam taką prostą sondę:

  1. <?
  2. ################################################################################
    #################
  3. #
  4. # Project : phpVote
  5. # File name : vote.php
  6. # Last Modified By : Erich Fuchs
  7. # e-mail : office@smartisoft.com
  8. # Purpose : Voting's
  9. # Version : 1.20
  10. #
  11. ################################################################################
    #################
  12.  
  13.  
  14. ################################################################################
    #################
  15. # Configuration
  16.  
  17. // mySQL Stuff
  18. $server = "sq..; // Your Host, most cases localhost
  19. $db_user = "d"; // Your mySql Username
  20. $db_pass = "n"; // Your mySql Password
  21. $database = "d"; // Your phpVote-Database
  22.  
  23.  
  24. // Size Stuff
  25. $vote_width = "200";
  26. $votebar_width = "80%";
  27. $votebar_height = "10";
  28.  
  29. // Language Stuff
  30. $vote_question = "Here comes the Question !?!";
  31. $vote_answer = "Answer";
  32. $vote_vote = "Vote";
  33. $vote_button = "Vote";
  34.  
  35. // Optical Stuff
  36. $htmlheader="
  37. <title>phpVote</title>
  38. <style type=text/css>
  39. body { background:#E2E2E2; margin: 10px; font-family: Verdana; }
  40. a:link { color:#333333; font-weight:bold; text-decoration:underline; font-family: Verdana; }
  41. a:visited { color:#555555; font-weight:bold; text-decoration:underline; font-family: Verdana; }
  42. a:active { color:#FFCC00; font-weight:bold; text-decoration:underline; font-family: Verdana; }
  43. a:hover { color:#FF9900; font-weight:bold; text-decoration:none; font-family: Verdana; }
  44. td.votebarout { background: #EEEEEE; border:0px; }
  45. td.votebarin { background: #FFCC00; border:0px; }
  46. td.votetext { font-size:8pt; }
  47. td.votequest { font-size:10pt; font-weight:bold; text-align:center;}
  48. td.votefooter { font-size:8pt; text-align:center; }
  49. div.votespace { font-size:4pt; text-align:center; }
  50. </style>
  51. ";
  52.  
  53. // Some Stuff
  54. $cookie_time = 12; // Vote Timeout in hours (set with Cookies)
  55. $ver = "1.20";
  56.  
  57.  
  58. # DO NOT CHANGE ANYTHING BEHIND THIS LINE !!!
  59. ################################################################################
    #################
  60.  
  61.  
  62. if ($vote && !$HelloCheater) {
  63. mysql_connect($server,$db_user,$db_pass) or die("Database Connect Error");
  64. $query = mysql_db_query($database,"update votes set votes=votes+1 where name='$vote'") or die("Database Query Error");
  65. mysql_close();
  66. setcookie("HelloCheater", "1", time()+(3600*$cookie_time));
  67. header("Location: $PHP_SELF");
  68. exit;
  69. }
  70. ?>
  71.  
  72. <DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  73.  
  74. <html>
  75. <head>
  76. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  77. <meta name="robots" content="index, nofollow">
  78. <meta name="revisit-after" content="20 days">
  79. <?
  80. echo $htmlheader;
  81. echo"</head><body>";
  82.  
  83. echo "<form action=\"$PHP_SELF\" method=\"POST\">";
  84. mysql_connect($server,$db_user,$db_pass) or die("Database Connect Error");
  85. $result=mysql_db_query($database,"select sum(votes) as sum from votes") or die("Database Query Error");;
  86. if($result) {
  87. $sum = (int) mysql_result($result,0,"sum");
  88. }
  89.  
  90. $result=mysql_db_query($database,"select * from votes order by votes DESC") or die("Database Query Error");;
  91. echo "<table border=0 width=$vote_width><tr><td class=\"votequest\" colspan=\"3\">$vote_question<br><br></td></tr><tr><td class=\"votetext\">$vote_vote</td><td class=\"votetext\">$vote_answer</td><td class=\"votetext\">%</td></tr>\n";
  92. while($row=mysql_fetch_row($result)) {
  93. echo "<tr><td align=center><input type=radio name=vote value=\"$row[0]\"></td>";
  94. echo "<td class=\"votetext\" colspan=\"2\">" .$row[0]."</td></tr><tr>
  95. <td class=\"votetext\" align=\"center\">".$row[1]."</td><td class=\"votetext\">";
  96.  
  97. if($sum && (int)$row[1]) {
  98. $per = (int)(100 * $row[1]/$sum);
  99.  
  100. echo "<table align=center border=0 cellspacing=0 cellpadding=1 width=\"$votebar_width\" height=\"$votebar_height\">\n";
  101. echo " <tr>\n";
  102. echo " <td class=\"votebarout\">\n";
  103. echo " <table align=left border=0 cellspacing=0 cellpadding=0 width=\"$per%\" height=\"100%\">\n";
  104. echo " <tr>\n";
  105. echo " <td class=\"votebarin\">\n";
  106. echo " <div class=\"votespace\">&nbsp;</div>\n";
  107. echo " </td>\n";
  108. echo " </tr>\n";
  109. echo " </table>\n";
  110. echo " </td>\n";
  111. echo " </tr>\n";
  112. echo "</table>\n";
  113.  
  114. echo"</td><td class=\"votetext\">$per</td>";
  115. }
  116. echo "</tr>\n";
  117. }
  118. echo "<tr><td class=\"votetext\" colspan=\"3\"><input type=submit value=\"$vote_button\"></form></td></tr>";
  119.  
  120. echo "</table>\n";
  121. echo "</body></html>";
  122. ?>



  1. #
  2. # Table structure for table 'phpVote'
  3. #
  4.  
  5. CREATE TABLE votes (
  6. name varchar(30) DEFAULT '0' NOT NULL,
  7. votes int(5),
  8. PRIMARY KEY (name)
  9. );
  10.  
  11. #
  12. # Dumping data for table 'phpVote'
  13. #
  14.  
  15. INSERT INTO votes VALUES ( 'Answer 1', '1');
  16. INSERT INTO votes VALUES ( 'Answer 2', '2');
  17. INSERT INTO votes VALUES ( 'Answer 3', '3');
  18. INSERT INTO votes VALUES ( 'Answer 4', '4');
  19.  


No i wszystko ładnie, tylko że po wybraniu opcji i kliknięciu na głosuj nic się nie zmienia.
Co może być tego powodem ?
Pilsener
A wyświetlają się jakieś błędy? I nic się nie zmienia to znaczy? Nie dodaje w ogóle głosów do bazy czy dodaje a nie wyświetla wyników na stronie? Wyświetla to cudo jakieś komunikaty?
mefistofeles
Nie wyświetla żadnych błędów.
Po zagłosowaniu nie jest aktualizowana w bazie kolumna: votes
i ciągle jest taka sama liczba głosów jak pierwotnie.
Co może być tego powodem ?
mortus
Daj zaraz po <?
  1. ini_set('display_errors', 1);
Rozumiem, że cudzysłów przy wklejaniu zgubiłeś w linii 18? Poza tym zamiast $vote w linii 62 spróbuj $_POST['vote'] (czyżby chodziło o register globals). No i $HelloCheater z cookies pobierane w celu zabezpieczenia przed ponownym głosowaniem również w linii 62 - zamiast $HelloCheater użyj $_COOKIE['HelloCheater'].
mefistofeles
Rzeczywiście były jakieś błędy.

Dodałem:

$vote=$_POST['vote'];
$HelloCheater=$_POST['HelloCheater'];

I teraz mam cośtakiego:

Notice: Undefined index: HelloCheater in /temp/sonda/votes.php on line 65
Warning: Cannot modify header information - headers already sent by (output started at /temp/sonda/votes.php:65) in /temp/sonda/votes.php on line 70
Notice: Undefined variable: PHP_SELF in /temp/sonda/votes.php on line 71
Warning: Cannot modify header information - headers already sent by (output started at /temp/sonda/votes.php:65) in /temp/sonda/votes.php on line 71

Nie wiem czym jest $HelloCheater.



nospor
helllocheater powinno bybc pobrane z ciasteczka
nie:$PHP_SELF a $_SERVER[' PHP_SELF']
mortus
Zamiast $_POST['HelloCheater'] ma być $_COOKIE['HelloCheater'].
nospor
tez dostanie notice'a a nastepnie warninga ze nie mozna wyslac naglowkow smile.gif
Najpierw trzeba sprawdzic czy jest w $_COOKIE a dopiero potem przypisac odpowiednią wartosc. A jak nie ma to ustawic na null
mefistofeles
Dziękuje za pomoc !

Co prawda mimo użycia $_SERVER['PHP_SELF'] dalej wyskakuje mi:

Notice: Undefined variable: PHP_SELF in /temp/sonda/votes.php on line 88

ale to już nieistotne...
nospor
skoro wyskakuje ci ten blad, znaczy ze jeszcze gdzies uzywasz zmiennej $PHP_SELF a konkretnie w pliku votes.php w linii 88
mefistofeles
Rzeczywiście ...
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.