magikern Skrevet 22. november 2003 Del Skrevet 22. november 2003 (endret) $poll_errarr = array(); echo("<div id=\"newsBlock\">\r\n<h2>Poll</h2>"); switch(@$HTTP_GET_VARS["a"]){ case "ShowAdd": GetNewPollDetails(); break; case "AddFinal": AddPoll(); break; case "ShowDelete": GetPollToDelete(); break; case "DeleteFinal": DeletePoll(); break; default: GetChoice(); } echo("<form name=\"frmAddPoll\" action=\"managepoll.php?method=AddFinal\" method=\"post\">"); global $question; global $answer1; global $answer2; global $answer3; global $answer4; global $answer5; /* Make sure the poll has a question and that at least two answer options were provided */ $numAnswers = 0; $err = ""; if($answer1 != "") { $numAnswers++; } if($answer2 != "") { $numAnswers++; } if($answer3 != "") { $numAnswers++; } if($answer4 != "") { $numAnswers++; } if($answer5 != "") { $numAnswers++; } if($answer6 != "") { $numAnswers++; } if($answer7 != "") { $numAnswers++; } if($answer8 != "") { $numAnswers++; } if($answer9 != "") { $numAnswers++; } if($answer10 != "") { $numAnswers++; } if($question == "") $err .= "<li>You didn't enter a title</li>"; if($numAnswers < 2) $err .= "<li>You must enter at least two answer choices</li>"; if($err != ""){ echo("<h1>Incomplete Fields</h1> You didn't complete all of the details for this poll. Take a look at the errors below and click the link below to go back and correct them: <ul>$err</ul> <a href=\"javascript:history.go(-1)\">Go Back</a>"); return; } $strQuery = mysql_db_query($db_name, "INSERT INTO pollQuestions (question,answer1,answer2,answer3,answer4,answer5,answer6,answer7,answer8,answer9,answer10) VALUES ('$question', '$answer1', '$answer2', '$answer3', '$answer4', '$answer5', '$answer6', '$answer7', '$answer8', '$answer9', '$answer10')"); if($strQuery){ echo("En ny Poll, \"$question\" er lagt til i databasen. <br /><a href=\"?s=poll\">Tilbake</a>"); }else{ array_push($poll_errarr, mysql_error()); } echo(" <form name=\"frmDelPoll\" action=\"?s=poll&a=DeleteFinal\" method=\"post\"> <select name=\"pollId\"> <option value=\"0\">-- Select Poll --</option>"); while($qRow = mysql_fetch_row($qResult)){ echo("<option value=\"".$qRow[0]."\">".$qRow[1]."</option>\r\n"); } echo("</select>\r\n</form>"); global $pollId; If($pollId > 0){ $strQuery1 = "DELETE FROM pollQuestions WHERE pk_Id = $pollId"; $strQuery2 = "DELETE FROM pollAnswers WHERE pollId = $pollId"; if(!mysql_query($strQuery1)){ array_push($poll_errarr,"Couldn't delete from pollQuestions table"); } if(!mysql_query($strQuery2)){ array_push($poll_errarr,"Couldn't delete from pollAnswers table"); } array_push($poll_errarr,"Poll Deleted Successfully"); array_push($poll_errarr,"You have successfully delete poll number $pollId from the polls database"); array_push($poll_errarr,"<a href=\"?s=poll\">Fortsett</a>"); }else{ array_push($poll_errarr,"No poll selected"); } $strQuery = mysql_db_query($db_name,"SELECT * FROM pollQuestions ORDER BY pk_Id DESC LIMIT 1"); echo("<form name=\"frmVote\" action=\"?s=poll&pollId=".$pRow["pk_Id"]."\" method=\"post\">"); for($i = 1; $i <= 5; $i++){ if($pRow["answer" . $i ] != ""){ echo("<input type=\"radio\" name=\"answer\" value=\"".$pRow["answer$i"]."\">".$pRow["answer" . $i]); } } echo("<input type=\"submit\" value=\"Vote Now\">"); global $pollId; global $answer; global $recentPollId; $strQuery = mysql_db_query($db_name,"SELECT pk_Id FROM pollQuestions ORDER BY pk_Id DESC LIMIT 1"); if($pRow = mysql_fetch_row($pResult)){ $recentPollId = $pRow[0]; }else{ die("No poll exists at this point in time."); } $visitorIP = realip(); // Did the user choose an option? if($answer == ""){ ShowPoll(); } // Has the user already voted? if(isset($HTTP_COOKIE_VARS["has_voted_$recentPollId"])){ ShowVotes(); return; } $strQuery = mysql_db_query($db_name,"INSERT INTO pollAnswers (pollid,answer,visitorIP) VALUES('$pollId, '$answer', '$visitorIP')"); if($strQuery){ setcookie("has_voted_" . $pollId, 1, time() + 3600 * 24 * 30); $strQuery = mysql_db_query($db_name,"SELECT * FROM pollQuestions pq INNER JOIN pollAnswers pa ON pq.pk_Id = pa.pollId WHERE pq.pk_Id = $recentPollId"); $answertally[0] = 0; $answertally[1] = 0; $answertally[2] = 0; $answertally[3] = 0; $answertally[4] = 0; $answertally[5] = 0; $answertally[6] = 0; $answertally[7] = 0; $answertally[8] = 0; $answertally[9] = 0; $answertotal = 0; while($pRow = mysql_fetch_array($pResult)){ switch($pRow["answer"]){ case $pRow["answer1"]: $answertally[0]++; break; case $pRow["answer2"]: $answertally[1]++; break; case $pRow["answer3"]: $answertally[2]++; break; case $pRow["answer4"]: $answertally[3]++; break; case $pRow["answer5"]: $answertally[4]++; break; case $pRow["answer6"]: $answertally[5]++; break; case $pRow["answer7"]: $answertally[6]++; break; case $pRow["answer8"]: $answertally[7]++; break; case $pRow["answer9"]: $answertally[8]++; break; case $pRow["answer10"]: $answertally[9]++; break; } } // Get the total number of votes for($i = 0; $i < sizeof($answertally); $i++) $answertotal += $answertally[$i]; @mysql_data_seek($pResult, 0); for($i = 1; $i <= 5; $i++){ if($pRow["answer$i"] != ""){ echo("<li>".$pRow["answer$i"]." [" . number_format(($answertally[$i-1] / $answertotal) * 100, 0, ".", '') . "%]" . "</li>"); echo("<img src=\"line.gif\" width=\""); echo($answertally[$i-1] / $answertotal) * 100;echo("\" height=\"5\"></li>"); } } echo ($answertally[$i-1] / $answertotal) * 100; number_format(($answertally[$i-1] / $answertotal) * 100, 0, ".", ''); echo("</div>"); ?> får parse error på siste linja her og det må jo bety at det er en ikkeavsluttet tag her et sted... jeg har lett ganske mye og finner ikke ut hvor den er håper kanskje en "klarsynt" klarer å finne ut hvor det er... Endret 22. november 2003 av magikern Lenke til kommentar
Gjest Slettet+432 Skrevet 22. november 2003 Del Skrevet 22. november 2003 echo ($answertally[$i-1] / $answertotal) * 100; såvidt jeg vet, så skal det vel bli echo ("($answertally[$i-1] / $answertotal) * 100;"); Lenke til kommentar
magikern Skrevet 23. november 2003 Forfatter Del Skrevet 23. november 2003 da fikk jeg problemer med at "']'" tydeligvis manglet så jeg prøvde: echo ("($answertally['$i-1'] / $answertotal) * 100;"); da fikk jeg: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' ps: man er grisedrita akkurat nå så det kan ha en viss innvirkning på feilene jeg fikk og ikke fant ut av akkurat nu... Lenke til kommentar
magikern Skrevet 26. november 2003 Forfatter Del Skrevet 26. november 2003 ingen som ser hvor feilen ligger? Lenke til kommentar
HP_H Skrevet 26. november 2003 Del Skrevet 26. november 2003 Her starter du blokken som du ikke avslutter. if($strQuery){ setcookie("has_voted_" . $pollId, 1, time() + 3600 * 24 * 30); Lenke til kommentar
magikern Skrevet 26. november 2003 Forfatter Del Skrevet 26. november 2003 hum sant nok, men nå får jeg: <b>Parse error</b>: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in <b>xxxXXXxxx</b> on line <b>203</b><br> og det er såvidt jeg kan telle meg fram til denne linja: for($i=1;$i<=5;$i++){ hvis jeg ikke tar helt feil pleier vel den mld å dukke opp ved skrivefeil o.l. i for loop'er, men jeg ser ingen feil der... Lenke til kommentar
magikern Skrevet 7. desember 2003 Forfatter Del Skrevet 7. desember 2003 (endret) ok fikk retta en god del feil, men står nå fast på linje 113 <?php echo("<div id=\"newsBlock\">\r\n<div class=\"space\" style=\"height: 2em;\"></div>\r\n"); global $HTTP_POST_VARS; global $HTTP_GET_VARS; global $HTTP_COOKIE_VARS; // Get the most recent poll id from the database $pResult = mysql_db_query($db_name, "SELECT pk_Id FROM pollQuestions ORDER BY pk_Id DESC LIMIT 1"); if($pRow = mysql_fetch_row($pResult)){ $recentPollId = $pRow[0]; }else{ array_push($drift_errarr,"No poll exists at this point in time."); echo errarr($drift_errarr); } if(isset($HTTP_GET_VARS["ViewPoll"])){ ShowVotes(); }elseif(!isset($HTTP_GET_VARS["pollId"])){ // Check whether or not this user has already voted if(!isset($HTTP_COOKIE_VARS["has_voted_" . $recentPollId])){ ShowPoll(); }else{ ShowVotes(); } }else{ AddVote(); } function ShowVotes(){ /* This function will display the voting poll but will display the results of the poll instead of the available answers */ global $recentPollId; $answertally[0] = 0; $answertally[1] = 0; $answertally[2] = 0; $answertally[3] = 0; $answertally[4] = 0; $answertotal = 0; if($pResult = mysql_db_query($db_name,"SELECT * FROM pollQuestions pq INNER JOIN pollAnswers pa ON pq.pk_Id = pa.pollId WHERE pq.pk_Id = $recentPollId")){ while($pRow = mysql_fetch_array($pResult)){ switch($pRow["answer"]){ case $pRow["answer1"]: $answertally[0]++; break; case $pRow["answer2"]: $answertally[1]++; break; case $pRow["answer3"]: $answertally[2]++; break; case $pRow["answer4"]: $answertally[3]++; break; case $pRow["answer5"]: $answertally[4]++; break; } } // Get the total number of votes for($i = 0; $i < sizeof($answertally); $i++) $answertotal += $answertally[$i]; @mysql_data_seek($pResult, 0); $pRow = mysql_fetch_array($pResult) or array_push($drift_errarr,"Coundn't view poll. You must vote first."); echo errarr($drift_errarr); }else{ array_push($drift_errarr,"An error occured while trying to display this polls results"); echo errarr($drift_errarr); } echo("<form name=\"frmVote\" action=\"?s=poll&pollId=" .$pRow['pk_Id']. "\" method=\"post\">\r\n<table>\r\n<tr><th>" .$pRow['question']. "</th></tr>"); for($i = 1; $i <= 5; $i++){ if($pRow['answer$i'] != ""){ echo("<tr>\r\n<td>"); echo $pRow['answer$i'] . " [" . number_format(($answertally[$i-1] / $answertotal) * 100, 0, ".", '') . "%]"; echo("<br /></td>\r\n<td><img src=\"line.gif\" width=\""); echo ($answertally[$i-1] / $answertotal) * 100; echo("\"></td>\r\n<td></td>\r\n</tr>"); } } echo("<tr><th>" . $answertotal . " vote" . ($answertotal == 1 ? "" : "s") . " so far</th></tr>\r\n</table>\r\n<br /><a href=\"".$_SERVER['HTTP_REFERER']."\">Tilbake</a>\r\n</form>"); } function AddVote(){ /* This function will add a vote to the pollAnswers table and then redirect the user to view the results of the poll */ global $pollId; global $answer; global $recentPollId; global $HTTP_SERVER_VARS; global $HTTP_COOKIE_VARS; $visitorIP = getip(); // Did the user choose an option? if($answer == ""){ ShowPoll(); } // Has the user already voted? if(isset($HTTP_COOKIE_VARS["has_voted_$recentPollId"])){ ShowVotes(); return; } if(mysql_db_query($db_name, "INSERT INTO pollAnswers VALUES(0, $pollId, '$answer', '$visitorIP')")){ setcookie("has_voted_" . $pollId, 1, time() + 3600 * 24 * 30); ShowVotes(); }else{ array_push($drift_errarr, "Voting Failed"); array_push($drift_errarr, "An internal error occured while trying to add your vote to the database."); array_push($drift_errarr, "<a href=\"?s=poll\">Tilbake</a>"); echo errarr($drift_errarr); } } function ShowPoll() { /* This function will display the latest poll which has been added to the database */ $pResult = mysql_db_query($db_name, "SELECT * FROM pollQuestions ORDER BY pk_Id DESC LIMIT 0, 1"); if($pRow = mysql_fetch_array($pResult)){ echo("<form name=\"frmVote\" action=\"?s=poll&pollId="); echo $pRow['pk_Id']; echo("\" method=\"post\">\r\n<table>\r\n<tr><th>"); echo $pRow['question']; echo("</th></tr>\r\n<tr><td>"); for($i = 1; $i <= 5; $i++){ if($pRow["answer" . $i ] != ""){ echo("<input type=\"radio\" name=\"answer\" value=\""); echo $pRow["answer$i"]; echo("\">"); echo $pRow["answer$i"]; echo("\">"); } } echo("<input type=\"submit\" value=\"Vote Now\"></td></tr>\r\n</table>\r\n</form>"); }else{ $drift_errarr = array(); array_push($drift_errarr, "An error occured while trying to display this poll"); echo errarr($drift_errarr); } } switch(@$HTTP_GET_VARS["a"]){ case "ShowAdd": GetNewPollDetails(); break; case "AddFinal": AddPoll(); break; case "ShowDelete": GetPollToDelete(); break; case "DeleteFinal": DeletePoll(); break; default: GetChoice(); } function DeletePoll(){ /* This function will delete the select poll from the pollQuestions database. It will also delete all answers for this poll from the pollAnswers table */ global $pollId; if($pollId > 0){ if(!mysql_query($db_name, "DELETE FROM pollQuestions WHERE pk_Id = $pollId")) array_push($drift_errarr, "Couldn't delete from pollQuestions table"); echo errarr($drift_errarr); if(!mysql_query($db_name, "DELETE FROM pollAnswers WHERE pollId = $pollId")) array_push($drift_errarr, "Couldn't delete from pollAnswers table"); echo errarr($drift_errarr); array_push($drift_errarr,"Poll Deleted Successfully"); array_push($drift_errarr,"You have successfully delete poll number $pollId from the polls database."); array_push($drift_errarr,"<a href=\"?s=poll\">Continue</a>"); echo errarr($drift_errarr); }else{ array_push($drift_errarr,"Incomplete Fields"); array_push($drift_errarr,"You didn't select a poll. Please use the link below to go back and select a poll."); array_push($drift_errarr,"<a href=\"/?s=poll\">Fortsett</a>"); echo errarr($drift_errarr); } } function GetPollToDelete(){ /* This function will allow the user to select a poll to delete from the database */ $qResult = mysql_query($db_name, "SELECT pk_Id, question FROM pollQuestions ORDER BY question ASC"); echo("<h2>Delete Poll</h2>\r\n<p>To delete a poll, please select one from the list below and click on the \"Delete Selected Poll\" button.</p> <form name=\"frmDelPoll\" action=\"?s=poll&a=DeleteFinal\" method=\"post\"> Poll To Delete: <select name=\"pollId\"> <option value=\"0\">-- Select Poll --</option>"); while($qRow = mysql_fetch_row($qResult)){ echo("<option value=\""); echo $qRow[0]; echo("\">"); echo $qRow[1]; echo("</option>"); } echo("</select>\r\n<input type=\"submit\" name=\"submit\" value=\"Delete Selected Poll\">\r\n</form>"); } function AddPoll(){ /* This function will actually add the new poll to the MySQL database. It includes some error checking to manke sure that all of the polls details were completed and that the INSERT query executed OK. */ global $question; global $answer1; global $answer2; global $answer3; global $answer4; global $answer5; // Make sure the poll has a question and that at least two // answer options were provided $numAnswers = 0; if($answer1 != "") { $numAnswers++; } if($answer2 != "") { $numAnswers++; } if($answer3 != "") { $numAnswers++; } if($answer4 != "") { $numAnswers++; } if($answer5 != "") { $numAnswers++; } global $poll_errarr; $poll_errarr = array(); if($question == "") array_push($poll_errarr, "You didn't enter a title</li>"); if($numAnswers < 2) array_push($poll_errarr, "You must enter at least two answer choices"); if($drift_errarr != ""){ array_push($drift_errarr,"Incomplete Fields"); array_push($drift_errarr,"You didn't complete all of the details for this poll. Take a look at the errors below and click the link below to go back and correct them:"); $pollerr = errarr($poll_errarr); array_push($drift_errarr, $pollerr); array_push($drift_errarr,"<a href=\"".$_SERVER['HTTP_REFERER']."\">Tilbake</a>"); echo errarr($drift_errarr); return; }else{ if(mysql_query($db_name, "INSERT INTO pollQuestions VALUES(0, '$question', '$answer1', '$answer2', '$answer3', '$answer4', '$answer5')")){ array_push($drift_errarr,"New Poll Created!"); array_push($drift_errarr,"A new poll, $question has been created successfully. The polls id is"); array_push($drift_errarr, mysql_insert_id()); array_push($drift_errarr, "Click on the link below to return to the main menu."); array_push($drift_errarr,"<a href=\"".$_SERVER['HTTP_REFERER']."\">Tilbake</a>"); echo errarr($drift_errarr); }else{ $drift_errarr = array(); array_push($drift_errarr, "An Error Occured"); array_push($drift_errarr, "An internal error occured while trying to create the new poll, $question "); array_push($drift_errarr,"<a href=\"".$_SERVER['HTTP_REFERER']."\">Tilbake</a>"); echo errarr($drift_errarr); } } } function GetNewPollDetails(){ /* This function uses a HTML form to grab the details of the new poll from the user */ echo("<form name=\"frmAddPoll\" action=\"?s=poll&a=AddFinal\" method=\"post\">\r\n <table>\r\n<tr><th>Poll Question:</th><td><input type=\"text\" name=\"question\" size=\"30\"></td></tr> <tr><th>Answer Option 1:</th><td><input type=\"text\" name=\"answer1\" maxlength=\"50\" size=\"30\"></td></tr> <tr><th>Answer Option 2:</th><td><input type=\"text\" name=\"answer2\" maxlength=\"50\" size=\"30\"></td></tr> <tr><th>Answer Option 3:</th><td><input type=\"text\" name=\"answer3\" maxlength=\"50\" size=\"30\"></td></tr> <tr><th>Answer Option 4:</th><td><input type=\"text\" name=\"answer4\" maxlength=\"50\" size=\"30\"></td></tr> <tr><th>Answer Option 5:</th><td><input type=\"text\" name=\"answer5\" maxlength=\"50\" size=\"30\"></td></tr> <tr><th><input type=\"submit\" name=\"submit\" value=\"Add New Poll Question\"></td></tr>\r\n</table>\r\n</form>"); } function GetChoice(){ /* This function simply displays a HTML page asking the user whether they would like to add or delete a poll */ echo("Please Choose An Option: <ul> <li><a href=\"?s=poll&a=ShowAdd\">Add A New Poll Question</a></li> <li><a href=\"?s=poll&a=ShowDelete\">Delete A Poll Question</a></li> <li><a href=\"?s=poll&ViewPoll=1\">View Current Poll</a></li> </ul>"); } echo("</div>"); ?> 112 og 113 har jeg kommer fram til at må være her: if($pResult = mysql_db_query($db_name,"SELECT * FROM pollQuestions pq INNER JOIN pollAnswers pa ON pq.pk_Id = pa.pollId WHERE pq.pk_Id = $recentPollId")){ while($pRow = mysql_fetch_array($pResult)){ har sjekket at det ikke er skrivefeil i forhold til det som står i db... ps: scriptet er fra en tutorial http://tinyurl.com/y2fo , men det var altså stappet fullt av forskjellige store og små feil... feilmld = Warning: Supplied argument is not a valid MySQL result resource in Endret 7. desember 2003 av magikern Lenke til kommentar
Neo Skrevet 7. desember 2003 Del Skrevet 7. desember 2003 skal ikke alle if-setningene dine inneholde "==" istedenfor "=" ? Lenke til kommentar
Torbjørn Skrevet 7. desember 2003 Del Skrevet 7. desember 2003 nei, ikke nødvendigvis. skriv ut mysql_error() etter den if-setningen. Lenke til kommentar
magikern Skrevet 7. desember 2003 Forfatter Del Skrevet 7. desember 2003 det viste seg at funksjonene ikke fant $db_name selvom den er satt til global i en fil som jeg har satt opp med require_once i alle filer... Lenke til kommentar
magikern Skrevet 7. desember 2003 Forfatter Del Skrevet 7. desember 2003 når jeg nå prøver å legge til en poll får jeg: Column count doesn't match value count at row 1 Lenke til kommentar
Torbjørn Skrevet 7. desember 2003 Del Skrevet 7. desember 2003 globale variable må defineres i funksjonen som vil hente ut den globale verdien. mysql feilen kommer fra en insert query, antall (kolonner) må matche antall (values) Lenke til kommentar
Anbefalte innlegg
Opprett en konto eller logg inn for å kommentere
Du må være et medlem for å kunne skrive en kommentar
Opprett konto
Det er enkelt å melde seg inn for å starte en ny konto!
Start en kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå