2 Replies - 768 Views - Last Post: 08 September 2009 - 10:20 PM Rate Topic: -----

#1 nashsclay  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 31-January 09

Invalid Poll Error Returned

Posted 08 September 2009 - 08:48 AM

Hello all. I am working on a poll. I get invalid poll returned. I'm using a scrpit I got from a book I recently bought. These are the files I'm using and what should go into the database.

vote_form.php
<?php
/* Display a vote form. */
require_once("vote_config.php");
$poll = $_GET['poll'];
if (!is_numeric($poll)) {
	die("Invalid poll");
}
/* Look up the poll in the database. */
$sql = "SELECT P.question, A.answer, A.answer_ID
	FROM poll P, answer A
	WHERE P.ID = $poll
	AND A.ID = P.ID";

$result = mysql_query($sql, $db) or die ("mysql error: " . mysql_error());
if (mysql_num_rows($result) == 0) {
	die('Invalid poll.');
}
/* If the user has already voted, show the results. */
if ($_COOKIE["poll_voted_$poll"]) {
	header("Location: vote_tally.php?poll=$poll");
	exit;
}
/* Vote form */
$question_list = "";
while($row = mysql_fetch_array($result)) {
	$question = $row['question'];
	$question_list .= '<li><input name="answer" type="radio" value="' . $row['answer_ID'] . '"> ' . $row['answer'] . '</li>';
}
?>
<html>
<head></head>
<body>
<span style="font-size: 12px;">
<span style="font-weight: bold; font-size: 14px;">
	Poll #<?php print $poll; ?>
</span><br />
<span style="font-weight: bold"><?php print $question; ?></span>
<from action="vote_process.php" method="post">
<ul style="list-style-type: none;">
<?php print $question_list; ?>
</ul>
<input name="poll" type="hidden" value="<?php print $poll; ?>">
<input name="" type="submit" value="Vote!">
</form>
</span>
</body></html>


vote_tally.php
<?php
/* Display the results of a poll. */
require_once("vote_config.php");

$poll = $_REQUEST['poll'];
if (!is_numeric($poll)) {
	die("Invalid poll");
}
/* Look up the question. */
$sql = "SELECT question
	FROM poll
	WHERE ID = $poll";
$result = @mysql_query($sql, $db) or die ("mysql error: " . mysql_error());
if (mysql_num_rows($result) != 1) {
	die('Invalid poll.');
}
$row = mysql_fetch_array($result);
$question = $row["question"];
$query = "SELECT count(*) AS num_total_votes
	FROM vote V
	WHERE V.ID = $poll";

$result = @mysql_query($query, $db) or die ("mysql error: " . mysql_error());
$row = mysql_fetch_array($result);
$num_total_votes = $row["num_total_votes"];
$query = "SELECT A.answer, A.answer_ID, count(V.answer_ID) as num_votes
	FROM answer A
LEFT JOIN vote V
	ON V.ID = A.ID
	AND V.answer_ID = A.answer_ID
	WHERE A.ID = $poll
	GROUP BY A.answer_ID
	ORDER BY num_votes DESC, A.answer ASC

";

$result = @mysql_query($query, $db) or die ("mysql error: " . mysql_error());
print "<html><head><title>Poll: $question</title></head><body>";
print '<ul style="list-style-type: none; font-size: 12px;">';
print '<li style="font-weight: bold; padding-bottom: 10px;">';
print "Poll #$poll: $question";
print '</li>';
while ($row = mysql_fetch_array($result)) {
if ($num_total_votes !=0) {
	$pct = sprintf("%.2f", 100.0 * $row["num_votes"] / $num_total_votes);
} else {
	$pct = "0";
}
$boxwidth = strval(1 + intval($pct)) . "px";
print '<li style="clear: left;">';
print "$row[answer]";
print "</li>";
print '<li style="clear: left; padding-bottom: 7px;">';
print '<div style="width: ' . $boxwidth . '; height: 15px;' . '; background: black; margin-right: 5px; float: left;">' . "</div>$pct%";
print '</li>';
}
print '<li style="clear: left;">';
print "Total Votes: $num_total_votes";
print '</li>';
print '</ul>';
print '</body></html>';
?>


vote_process.php
<?php
require_once("vote_config.php");

$poll = $_POST['poll'];
$answer = $_POST['answer'];
if (!is_numeric($poll )|| !is_numeric($answer)) {
	die("Invalid poll or answer");
}
/* Look up the poll and answer. */
$sql = "SELECT A.answer_ID
	FROM poll P, answer A
	WHERE P.ID, = A.ID
	AND P.ID = $poll
	AND A.answer_ID = $answer";
	$result = @mysql_query($sql, $db) or die (mysql_error());
	if (mysql_num_rows($result) == 0) {
		die('Invalid poll or answer.');
}
/* Check for prior votes. */
if (!$_COOKIE["poll_voted_$poll"]) {
	/* Insert the vote. */
	$sql = "INSERT INTO `vote` (`answer_ID` , `ID`)
		VALUES ($answer, $poll);";
	$result = @mysql_query($sql, $db) or
		die ("Couldn't insert: " . mysql_error());
/* Mark the poll as voted. */
setcookie("poll_voted_$poll", "1", time() + (60*60*24 * 30));
}
/* Redirect to poll results. */
header("Location: vote_tally.php?poll=$poll");
?>


vote_config.php
<?php
$db = @mysql_connect("xx.xx.xx.xx", "xzeroadds", "xxxxxxx") or
	die("Couldn't connect.");
@mysql_select_db("xzeroadds", $db) or die("Couldn't select database.");
?>


And finally the include files for the database
CREATE TABLE `poll` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`question` MEDIUMTEXT NOT NULL ,
PRIMARY KEY ( `ID` )
) TYPE = MYISAM;

CREATE TABLE `answer` (
`answer_ID` INT NOT NULL AUTO_INCREMENT ,
`ID` INT NOT NULL ,
`answer` MEDIUMTEXT NOT NULL ,
PRIMARY KEY (`answer_ID` )
) TYPE = MYISAM;

CREATE TABLE `vote` (
`ID` INT NOT NULL ,
`answer_ID` INT NOT NULL ,
INDEX (`ID`)
) TYPE = MYISAM;

INSERT INTO poll (question) VALUES ("What do you think of the site?");
INSERT INTO answer (ID, answer) VALUES (1, "This site is great!");
INSERT INTO answer (ID, answer) VALUES (1, "This site is ok.");
INSERT INTO answer (ID, answer) VALUES (1, "I'm not going to use this site.");


Is This A Good Question/Topic? 0
  • +

Replies To: Invalid Poll Error Returned

#2 young.steveo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 14-May 09

Re: Invalid Poll Error Returned

Posted 08 September 2009 - 07:32 PM

It looks like right at the beginning of the vote form you are checking to see if the $_GET variable is numeric, and killing the script if it is not. The first time you load the page, the $_GET variable is empty, and therefore it is not numeric, and therefore the script dies. You should use if (isset($_GET['poll'])) { } around the numeric check.


if that doesn't work then your database query is failing. I've noticed that you are using the same error message 'Invalid poll' in several places. So there's no telling where the error is arising from. You should change the die message to something unique in order to track down the error.

View Postnashsclay, on 8 Sep, 2009 - 07:48 AM, said:

Hello all. I am working on a poll. I get invalid poll returned. I'm using a scrpit I got from a book I recently bought. These are the files I'm using and what should go into the database.

vote_form.php
<?php
/* Display a vote form. */
require_once("vote_config.php");
$poll = $_GET['poll'];
if (!is_numeric($poll)) {
	die("Invalid poll");
}
/* Look up the poll in the database. */
$sql = "SELECT P.question, A.answer, A.answer_ID
	FROM poll P, answer A
	WHERE P.ID = $poll
	AND A.ID = P.ID";

$result = mysql_query($sql, $db) or die ("mysql error: " . mysql_error());
if (mysql_num_rows($result) == 0) {
	die('Invalid poll.');
}
/* If the user has already voted, show the results. */
if ($_COOKIE["poll_voted_$poll"]) {
	header("Location: vote_tally.php?poll=$poll");
	exit;
}
/* Vote form */
$question_list = "";
while($row = mysql_fetch_array($result)) {
	$question = $row['question'];
	$question_list .= '<li><input name="answer" type="radio" value="' . $row['answer_ID'] . '"> ' . $row['answer'] . '</li>';
}
?>
<html>
<head></head>
<body>
<span style="font-size: 12px;">
<span style="font-weight: bold; font-size: 14px;">
	Poll #<?php print $poll; ?>
</span><br />
<span style="font-weight: bold"><?php print $question; ?></span>
<from action="vote_process.php" method="post">
<ul style="list-style-type: none;">
<?php print $question_list; ?>
</ul>
<input name="poll" type="hidden" value="<?php print $poll; ?>">
<input name="" type="submit" value="Vote!">
</form>
</span>
</body></html>


vote_tally.php
<?php
/* Display the results of a poll. */
require_once("vote_config.php");

$poll = $_REQUEST['poll'];
if (!is_numeric($poll)) {
	die("Invalid poll");
}
/* Look up the question. */
$sql = "SELECT question
	FROM poll
	WHERE ID = $poll";
$result = @mysql_query($sql, $db) or die ("mysql error: " . mysql_error());
if (mysql_num_rows($result) != 1) {
	die('Invalid poll.');
}
$row = mysql_fetch_array($result);
$question = $row["question"];
$query = "SELECT count(*) AS num_total_votes
	FROM vote V
	WHERE V.ID = $poll";

$result = @mysql_query($query, $db) or die ("mysql error: " . mysql_error());
$row = mysql_fetch_array($result);
$num_total_votes = $row["num_total_votes"];
$query = "SELECT A.answer, A.answer_ID, count(V.answer_ID) as num_votes
	FROM answer A
LEFT JOIN vote V
	ON V.ID = A.ID
	AND V.answer_ID = A.answer_ID
	WHERE A.ID = $poll
	GROUP BY A.answer_ID
	ORDER BY num_votes DESC, A.answer ASC

";

$result = @mysql_query($query, $db) or die ("mysql error: " . mysql_error());
print "<html><head><title>Poll: $question</title></head><body>";
print '<ul style="list-style-type: none; font-size: 12px;">';
print '<li style="font-weight: bold; padding-bottom: 10px;">';
print "Poll #$poll: $question";
print '</li>';
while ($row = mysql_fetch_array($result)) {
if ($num_total_votes !=0) {
	$pct = sprintf("%.2f", 100.0 * $row["num_votes"] / $num_total_votes);
} else {
	$pct = "0";
}
$boxwidth = strval(1 + intval($pct)) . "px";
print '<li style="clear: left;">';
print "$row[answer]";
print "</li>";
print '<li style="clear: left; padding-bottom: 7px;">';
print '<div style="width: ' . $boxwidth . '; height: 15px;' . '; background: black; margin-right: 5px; float: left;">' . "</div>$pct%";
print '</li>';
}
print '<li style="clear: left;">';
print "Total Votes: $num_total_votes";
print '</li>';
print '</ul>';
print '</body></html>';
?>


vote_process.php
<?php
require_once("vote_config.php");

$poll = $_POST['poll'];
$answer = $_POST['answer'];
if (!is_numeric($poll )|| !is_numeric($answer)) {
	die("Invalid poll or answer");
}
/* Look up the poll and answer. */
$sql = "SELECT A.answer_ID
	FROM poll P, answer A
	WHERE P.ID, = A.ID
	AND P.ID = $poll
	AND A.answer_ID = $answer";
	$result = @mysql_query($sql, $db) or die (mysql_error());
	if (mysql_num_rows($result) == 0) {
		die('Invalid poll or answer.');
}
/* Check for prior votes. */
if (!$_COOKIE["poll_voted_$poll"]) {
	/* Insert the vote. */
	$sql = "INSERT INTO `vote` (`answer_ID` , `ID`)
		VALUES ($answer, $poll);";
	$result = @mysql_query($sql, $db) or
		die ("Couldn't insert: " . mysql_error());
/* Mark the poll as voted. */
setcookie("poll_voted_$poll", "1", time() + (60*60*24 * 30));
}
/* Redirect to poll results. */
header("Location: vote_tally.php?poll=$poll");
?>


vote_config.php
<?php
$db = @mysql_connect("xx.xx.xx.xx", "xzeroadds", "xxxxxxx") or
	die("Couldn't connect.");
@mysql_select_db("xzeroadds", $db) or die("Couldn't select database.");
?>


And finally the include files for the database
CREATE TABLE `poll` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`question` MEDIUMTEXT NOT NULL ,
PRIMARY KEY ( `ID` )
) TYPE = MYISAM;

CREATE TABLE `answer` (
`answer_ID` INT NOT NULL AUTO_INCREMENT ,
`ID` INT NOT NULL ,
`answer` MEDIUMTEXT NOT NULL ,
PRIMARY KEY (`answer_ID` )
) TYPE = MYISAM;

CREATE TABLE `vote` (
`ID` INT NOT NULL ,
`answer_ID` INT NOT NULL ,
INDEX (`ID`)
) TYPE = MYISAM;

INSERT INTO poll (question) VALUES ("What do you think of the site?");
INSERT INTO answer (ID, answer) VALUES (1, "This site is great!");
INSERT INTO answer (ID, answer) VALUES (1, "This site is ok.");
INSERT INTO answer (ID, answer) VALUES (1, "I'm not going to use this site.");

Was This Post Helpful? 0
  • +
  • -

#3 nashsclay  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 31-January 09

Re: Invalid Poll Error Returned

Posted 08 September 2009 - 10:20 PM

I switched the code that you told me now were further. Now I get this.

Quote

mysql error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND A.ID = P.ID' at line 4

Ya I did change some of those error codes to find the right ones but I guess that's what you get from a book. Now for the new error.

EDIT: Ok so I figured out the code problem. I had to add the ' symbol to some places. Now all that shows up is Poll # and the vote button as shown here. http://www.catchpenn...m/vote_form.php Why is it not pulling the information from the database correctly? :unsure:

This post has been edited by nashsclay: 09 September 2009 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1