Hi All,
I am following a poll tutorial located Here..., but I seem to be having 1 problem,
the poll works, but does not output the GD graph images, Now I know I have GD Library installed and its working correctly,
I think the problem is down to this one mysql statement,
$num_votes_query = 'SELECT SUM(votes) AS sumvotes
FROM poll_answers';
$num_votes_query .= ' WHERE poll_num=1';
I have run this statment in phpmyadmin and it does not work!
Where poll_num=1 what is poll_num ? is it suppose to be a cell ?, I have not got this cell in my database, and nor is it on the tutorial,
here is my 3 pages of this tutorial,
Page1 =bargraph.php
<?php
$height = 10;
$width = 100;
$im = imagecreate($width, $height);
$bg = imagecolorallocate($im, 255, 255, 255); // white
$fg = imagecolorallocate($im, 255, 0, 0); // red
imagefilledrectangle($im, 0, 0, $_GET['pct'], $height, $fg);
imagerectangle($im, 0, 0, $width, $height, $bg);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Page 2 = poll.php
<?php
$host = 'localhost';
$user = 'XXXXX';
$pass = 'XXXXX';
$db = 'polls';
print "<html>\n";
print "<head>\n";
print "<title>Grand Old Opera Poll</title>\n";
print "</head>\n";
print "<body>\n";
print "<form action=\"showvotes.php\" method=\"post\">\n";
print "<p>Which of these activities do you most enjoy?</p>\n";
$dbcon = mysql_connect($host, $user, $pass)
or die('Unable to connect to server ' . $host);
mysql_select_db($db) or die('Unable to find database ' . $db);
$form_query = 'SELECT * FROM poll_answers';
if($result = mysql_query($form_query)) {
while($row = mysql_fetch_array($result)) {
print "<input type=\"radio\" name=\"vote\" value=" . $row['choice'] . ">". $row['activity'] . "<br />\n";
}
}
print "<input type=\"submit\" value=\"vote\">\n";
print "</form>\n";
print "<p><a href=\"showvotes.php\">See vote totals</a></p>\n";
print "</body>\n";
print "</html>\n";
?>
page 3 = showvotes.php
<?php
$host = 'localhost';
$user = 'XXXXX';
$pass = 'XXXXX';
$db = 'polls';
print "<html>\n";
print "<head>\n";
print "<title>Vote totals</title>\n";
print "</head>\n";
print "<body>\n";
$dbcon = mysql_connect($host, $user, $pass)
or die('Unable to connect to server ' . $host);
mysql_select_db($db) or die('Unable to find database ' . $db);
if(isset($_POST['vote']) && ctype_digit($_POST['vote'])) {
$query = 'UPDATE poll_answers SET votes=votes+1
WHERE choice=' . $_POST['vote'];
$result = mysql_query($query);
}
//
$num_votes_query = 'SELECT SUM(votes) AS sumvotes
FROM poll_answers';
$num_votes_query .= ' WHERE poll_num=1';
if ($result = mysql_query($num_votes_query)) {
$row = mysql_fetch_array($result);
$sum = $row['sumvotes'];
}
$totals_query = 'SELECT activity, votes FROM poll_answers ';
if ($result = mysql_query($totals_query)) {
print "<table>\n";
print "<tr><th>Activity</th>\n";//THIS IS GETTTING CALLED
print "<th colspan=\"2\" align=\"center\">Votes</th></tr>\n";
while($row = mysql_fetch_array($result)) {
print "<tr><td>" . $row['activity'] . "</td>\n";
print "<td align=\"right\">" . $row['1'] . "</td>\n";
if($sum) {
$percent = round($row['votes'] * 100 / $sum);
print "</tr>\n<tr><td>";
print "<img src=\"bargraph.php?pct=$percent\"></td>\n";
print "<td align=\"right\">" . $percent . "%</td>\n";
}
print "</tr>\n";
}
print "</table>\n";
}
?>
SQL create DB
CREATE DATABASE polls;
USE polls;
CREATE TABLE poll_answers
(choice INT NOT NULL PRIMARY KEY,
activity VARCHAR(35) NOT NULL, votes INT DEFAULT 0);
INSERT INTO poll_answers(choice, activity, votes) VALUES(1, "Going to the opera", 4),
(2, "Listening to opera music on CD", 2),
(3, "Getting bitten by a ferret", 14);
GRANT SELECT, UPDATE ON polls.* TO user IDENTIFIED BY 'pass';
I would love it if someone could help us out on this,
Many Thanks And Sorry For The Long Post